﻿using DHQGHN.DAL.BO.DHQG;
using DHQGHN.DAL.Models.DHQG;
using DHQGHN.PM2.Models;
using DHQGHN.PM2.Models.DHQG;
using FlexCel.Core;
using FlexCel.Render;
using FlexCel.XlsAdapter;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DHQGHN.PM2.Controllers
{
    public class BaoCaoController : BaseController
    {
        #region KHAI BAO BIEN
        NhomDonViBO nhomdvbo = new NhomDonViBO();
        DonViBO donvibo = new DonViBO();
        KeHoachBO kehoachbo = new KeHoachBO();
        KehoachThuchienBO kehoachthuchienbo = new KehoachThuchienBO();
        ChiTieuBO chitieubo = new ChiTieuBO();
        bool isLandScape = false;
        double landscapeSize = 203;
        double portraitSize = 140;
        double dvtColumnWidth = 12;
        double dvColumnWidth = 6;
        decimal topMargin = 0.2m;
        decimal bottomMargin = 0.2m;
        decimal leftMargin = 0.5m;
        decimal rightMargin = 0.2m;
        int startRow = 4;
        int startCol = 1;
        #endregion

        private class TongGiaTri
        {
            public int RowIndex { get; set; }
            public string DonViTinh { get; set; }
            public double GiaTri { get; set; }
        }

        List<TongGiaTri> lstTGT;

        #region KET XUAT EXCEL PDF 3 BAO CAO
        public FileResult ExpordFullData(int namKHId, int nhomdv, int baocao)
        {
            if (baocao == 0)
            {
                try
                {
                    ExcelPackage p = this.getExcelPackMucDoHoanThanhTheoMang(namKHId, nhomdv);

                    FileResult result = File(p.GetAsByteArray(), "application/octet-stream", "MucDoHoanThanhTheoMang.xlsx");
                    return result;

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            else
            {
                try
                {
                    ExcelPackage p = this.getExcelPackMucDoHoanThanhTheoDV(namKHId, nhomdv);
                    //string v_filename = "MucDoHoanThanhTheoDV";

                    //MemoryStream stream = new MemoryStream();
                    //ZipOutputStream zipstream = new ZipOutputStream(stream);
                    //zipstream.SetLevel(3); //0-9, 9 being the highest level of compression

                    //// loops through the PDFs I need to create
                    //var newEntry = new ZipEntry("MucHoanThanh.xlsx"); //vanduong, 16/7/2015
                    //newEntry.DateTime = DateTime.Now;
                    //zipstream.PutNextEntry(newEntry);
                    //MemoryStream inStream = new MemoryStream(p.GetAsByteArray());
                    //StreamUtils.Copy(inStream, zipstream, new byte[4096]);
                    //inStream.Close();
                    //zipstream.CloseEntry();

                    //zipstream.IsStreamOwner = false;
                    //zipstream.Close();
                    //stream.Position = 0;

                    //p.Dispose();
                    //return File(stream.ToArray(), "application/octet-stream", "DHQG-" + v_filename + "-.zip");
                    FileResult result = File(p.GetAsByteArray(), "application/octet-stream", "MucHoanThanh.xlsx");
                    return result;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        //phunx1, 20160507: in 3 bao cao pdf
        public FileResult ViewReportPdf(int namKHId, int nhomdv, int baocao)
        {
            if (baocao == 0)
            {
                ExcelPackage p = this.getExcelPackMucDoHoanThanhTheoMang(namKHId, nhomdv);


                MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

                //phunx1, 20160422: xu ly open pdf
                XlsFile Result11 = new XlsFile(1, true);

                Result11.Open(inStream);
                Result11.PrintPaperSize = TPaperSize.A4;
                if (!isLandScape) Result11.PrintLandscape = false;
                else Result11.PrintLandscape = true;
                Result11.AutofitRowsOnWorkbook(true, true, 1);
                using (FlexCelPdfExport pdf = new FlexCelPdfExport())
                {
                    pdf.Workbook = Result11;
                    pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        pdf.BeginExport(ms);
                        pdf.ExportAllVisibleSheets(true, "BaoCaoTuan");
                        pdf.EndExport();
                        ms.Position = 0;
                        return File(ms.ToArray(), "application/pdf");
                    }
                }
                p.Dispose();
                inStream.Dispose();
            }
            else
            {
                ExcelPackage p = this.getExcelPackMucDoHoanThanhTheoDV(namKHId, nhomdv);
                MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

                //phunx1, 20160422: xu ly open pdf
                XlsFile Result11 = new XlsFile(1, true);

                Result11.Open(inStream);
                Result11.PrintPaperSize = TPaperSize.A4;
                if (!isLandScape) Result11.PrintLandscape = false;
                else Result11.PrintLandscape = true;
                Result11.AutofitRowsOnWorkbook(true, true, 1);
                using (FlexCelPdfExport pdf = new FlexCelPdfExport())
                {
                    pdf.Workbook = Result11;
                    pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        pdf.BeginExport(ms);
                        pdf.ExportAllVisibleSheets(true, "BaoCaoTuan");
                        pdf.EndExport();
                        ms.Position = 0;
                        return File(ms.ToArray(), "application/pdf");
                    }
                }
                p.Dispose();
                inStream.Dispose();
            }
            return null;
        }

        private ExcelPackage getExcelPackMucDoHoanThanhTheoDV(int namKHId, int nhomdv)
        {
            var dv = nhomdvbo.GetById(nhomdv);
            string filepath = Server.MapPath("~/TemplateExcel/BM2.xlsx");
            //string v_filename = "MucDoHoanThanhTheoDV";
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);

            ExcelPackage returnPackage = new ExcelPackage();
            int currIndex = 0;
            try
            {
                ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet wd = p.Workbook.Worksheets[2];

                #region Set Margion
                wd.PrinterSettings.TopMargin = 0.5m;
                wd.PrinterSettings.BottomMargin = 0.5m;
                wd.PrinterSettings.LeftMargin = 0.7m;
                wd.PrinterSettings.RightMargin = 0.3m;
                wd.PrinterSettings.FitToWidth = 1;
                #endregion

                wd.Name = "Mức độ hoàn thành nhóm " + dv.TenNhomDonVi;
                int i2 = 7;
                int stt2 = 1;
                var lstdv = donvibo.GetAllDonViByNhom(nhomdv);
                wd.Cells[4, 1].Value = "Khối đơn vị " + dv.TenNhomDonVi;
                int total = 0;
                int totallonhon100 = 0;
                int totalbang100 = 0;
                int totallonhon50 = 0;
                int totalnhohon50 = 0;
                int countlonhon100 = 0;
                int countbang100 = 0;
                int countlonhon50 = 0;
                int countnhohon50 = 0;
                string errDV = "";

                foreach (var item in lstdv)
                {
                    var lstkhdv = kehoachbo.GetbyDV(namKHId, 31, item.DonViId);
                    var lstkhthdv = kehoachthuchienbo.GetallByDV(namKHId, item.DonViId);


                    if (lstkhdv.Count != lstkhthdv.Count)
                    {
                        errDV = errDV + item.DonViId.ToString() + ",";
                        continue;
                    }

                    countlonhon100 = 0;
                    countbang100 = 0;
                    countlonhon50 = 0;
                    countnhohon50 = 0;
                    wd.Cells[i2, 1].Value = stt2;
                    wd.Cells[i2, 2].Value = item.TenDonVi;
                    wd.Cells[i2, 3].Value = lstkhdv.Count;
                    if (lstkhthdv.Count == 0)
                    {
                        countnhohon50 = lstkhdv.Count;
                    }
                    else
                    {
                        for (int g = 0; g < lstkhthdv.Count; g++)
                        {
                            currIndex = g;
                            if (lstkhthdv[g].GIA_TRI == null || lstkhdv[g].GIA_TRI == null)
                            {
                                countnhohon50 += 1;
                            }
                            else
                            {
                                try
                                {
                                    double a = double.Parse(lstkhthdv[g].GIA_TRI);
                                    double b = double.Parse(lstkhdv[g].GIA_TRI);
                                    if (b == 0)
                                    {
                                        countnhohon50 += 1;
                                    }
                                    else
                                    {
                                        double c = Math.Round((a / b) * 100);
                                        if (c > 100)
                                        {
                                            countlonhon100 += 1;
                                        }
                                        if (c == 100)
                                        {
                                            countbang100 += 1;
                                        }
                                        if (c >= 50 && c < 100)
                                        {
                                            countlonhon50 += 1;
                                        }
                                        if (c < 50)
                                        {
                                            countnhohon50 += 1;
                                        }
                                    }
                                }
                                catch
                                {
                                    //Ghi log lại
                                    continue;
                                }
                            }
                        }
                    }
                    wd.Cells[i2, 4].Value = countlonhon100;
                    wd.Cells[i2, 5].Value = countbang100;
                    wd.Cells[i2, 6].Value = countlonhon50;
                    wd.Cells[i2, 7].Value = countnhohon50;
                    total += lstkhdv.Count;
                    totallonhon100 += countlonhon100;
                    totalbang100 += countbang100;
                    totallonhon50 += countlonhon50;
                    totalnhohon50 += countnhohon50;
                    i2 += 1;
                    stt2 += 1;
                }
                wd.Cells[i2, 2].Value = "Tổng";
                wd.Cells[i2, 3].Value = total;
                wd.Cells[i2, 4].Value = totallonhon100;
                wd.Cells[i2, 5].Value = totalbang100;
                wd.Cells[i2, 6].Value = totallonhon50;
                wd.Cells[i2, 7].Value = totalnhohon50;
                wd.Cells[7, 1, i2, 7].Style.Border.Left.Style = ExcelBorderStyle.Thin;
                wd.Cells[7, 1, i2, 7].Style.Border.Top.Style = ExcelBorderStyle.Thin;
                wd.Cells[7, 1, i2, 7].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                wd.Cells[7, 1, i2, 7].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                wd.Cells[7, 1, i2, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                wd.Cells[7, 1, i2, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                wd.Cells[7, 3, i2, 7].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                wd.Cells[7, 3, i2, 7].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                wd.Cells[7, 1, i2, 7].Style.Font.Name = "Times New Roman";
                wd.Cells[i2, 1, i2, 7].Style.Font.Bold = true;

                return p;
            }
            catch (Exception ex)
            {
                Console.WriteLine(currIndex);
                throw;
            }
            return returnPackage;
        }

        private ExcelPackage getExcelPackMucDoHoanThanhTheoMang_Old(int namKHId, int nhomdv)
        {
            string filepath = Server.MapPath("~/TemplateExcel/BM1.xlsx");
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);

            ExcelPackage returnPackage = new ExcelPackage();

            //using (ExcelPackage p = new ExcelPackage(fileInfo, true))
            //{
            try
            {
                ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = p.Workbook.Worksheets[2];

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion


                ws.Name = "Mức độ hoàn thành theo mảng";
                var lsttenchitieu = chitieubo.GETCAP1(namKHId);
                int i = 7;
                int stt = 1;
                var lstkh = kehoachbo.GetSumKH(namKHId, 31);
                var lskhth = kehoachthuchienbo.GetSumKehoachthuchien(namKHId, 0);
                int total = 0;
                int totallonhon100 = 0;
                int totalbang100 = 0;
                int totallonhon50 = 0;
                int totalnhohon50 = 0;
                int counttotal = 0;
                int countlonhon100 = 0;
                int countbang100 = 0;
                int countlonhon50 = 0;
                int countnhohon50 = 0;
                foreach (var item in lsttenchitieu)
                {
                    counttotal = 0;
                    countlonhon100 = 0;
                    countbang100 = 0;
                    countlonhon50 = 0;
                    countnhohon50 = 0;
                    ws.Cells[i, 1].Value = stt;
                    ws.Cells[i, 2].Value = item.TEN_CHI_TIEU;
                    for (int g = 0; g < lstkh.Count; g++)
                    {
                        if (lstkh[g].MA_CAP1 == item.MA_CAP1)
                        {
                            counttotal += 1;
                            double a = lskhth[g].GIA_TRI;
                            double b = lstkh[g].GIA_TRI;
                            if (b == 0)
                            {
                                countnhohon50 += 1;
                            }
                            else
                            {
                                double c = Math.Round((a / b) * 100);
                                if (c > 100)
                                {
                                    countlonhon100 += 1;
                                }
                                if (c == 100)
                                {
                                    countbang100 += 1;
                                }
                                if (c >= 50 && c < 100)
                                {
                                    countlonhon50 += 1;
                                }
                                if (c < 50)
                                {
                                    countnhohon50 += 1;
                                }
                            }
                        }
                    }
                    ws.Cells[i, 3].Value = counttotal;
                    ws.Cells[i, 4].Value = countlonhon100;
                    ws.Cells[i, 5].Value = countbang100;
                    ws.Cells[i, 6].Value = countlonhon50;
                    ws.Cells[i, 7].Value = countnhohon50;
                    total += counttotal;
                    totallonhon100 += countlonhon100;
                    totalbang100 += countbang100;
                    totallonhon50 += countlonhon50;
                    totalnhohon50 += countnhohon50;
                    i += 1;
                    stt += 1;
                }
                ws.Cells[i, 2].Value = "Tổng";
                ws.Cells[i, 3].Value = total;
                ws.Cells[i, 4].Value = totallonhon100;
                ws.Cells[i, 5].Value = totalbang100;
                ws.Cells[i, 6].Value = totallonhon50;
                ws.Cells[i, 7].Value = totalnhohon50;
                ws.Cells[7, 1, i, 7].Style.Border.Left.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 7].Style.Border.Top.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 7].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 7].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[7, 1, i, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells[7, 3, i, 7].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[7, 3, i, 7].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells[7, 1, i, 7].Style.Font.Name = "Times New Roman";
                ws.Cells[i, 1, i, 7].Style.Font.Bold = true;

                return p;
            }
            catch (Exception ex)
            {
                throw;
            }
            //}
            return returnPackage;

        }

        private ExcelPackage getExcelPackMucDoHoanThanhTheoMang(int namKHId, int nhomdv)
        {
            string filepath = Server.MapPath("~/TemplateExcel/BM1.xlsx");
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);

            ExcelPackage returnPackage = new ExcelPackage();

            try
            {
                ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = p.Workbook.Worksheets[2];

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion


                ws.Name = "Mức độ hoàn thành theo mảng";
                var lsttenchitieu = chitieubo.GETCAP1(namKHId);
                int i = 7;
                int stt = 1;

                //var lstkh = kehoachbo.GetSumKH(namKHId, 31);
                //var lskhth = kehoachthuchienbo.GetSumKehoachthuchien(namKHId, 0);

                int laChiTieuChinhThuc = 31;
                int laChiTieuDieuChinh = 61;
                ExportExcellBO sqlBO = new ExportExcellBO();
                List<ExportExcell> chiTieuChinhThucDHQG = new List<ExportExcell>();
                bool hasDieuChinh = sqlBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

                if (hasDieuChinh) chiTieuChinhThucDHQG = sqlBO.GetChiTieuDinhHuong(namKHId, laChiTieuDieuChinh);
                else chiTieuChinhThucDHQG = sqlBO.GetChiTieuDinhHuong(namKHId, laChiTieuChinhThuc);

                List<double> tongThucHien = TongThucHien(namKHId);


                int total = 0;
                int totallonhon100 = 0;
                int totalbang100 = 0;
                int totallonhon50 = 0;
                int totalnhohon50 = 0;
                int counttotal = 0;
                int countlonhon100 = 0;
                int countbang100 = 0;
                int countlonhon50 = 0;
                int countnhohon50 = 0;


                foreach (var item in lsttenchitieu)
                {
                    counttotal = 0;
                    countlonhon100 = 0;
                    countbang100 = 0;
                    countlonhon50 = 0;
                    countnhohon50 = 0;

                    for (int j = 0; j < chiTieuChinhThucDHQG.Count; j++)
                    {
                        if ((chiTieuChinhThucDHQG[j].CAP.Equals(3)) && (chiTieuChinhThucDHQG[j].MA_CAP1.Equals(item.MA_CAP1)))
                        {
                            counttotal += 1;
                            double a = tongThucHien[j];
                            double b = chiTieuChinhThucDHQG[j].GIA_TRI;

                            if (b == 0)
                            {
                                countnhohon50 += 1;
                            }
                            else
                            {
                                double c = Math.Round((a / b) * 100, MidpointRounding.AwayFromZero);
                                if (c > 100)
                                {
                                    countlonhon100 += 1;
                                }
                                if (c == 100)
                                {
                                    countbang100 += 1;
                                }
                                if (c >= 50 && c < 100)
                                {
                                    countlonhon50 += 1;
                                }
                                if (c < 50)
                                {
                                    countnhohon50 += 1;
                                }
                            }
                        }
                    }

                    ws.Cells[i, 1].Value = stt;
                    ws.Cells[i, 2].Value = item.TEN_CHI_TIEU;
                    ws.Cells[i, 3].Value = counttotal;
                    ws.Cells[i, 4].Value = countlonhon100;
                    ws.Cells[i, 5].Value = countbang100;
                    ws.Cells[i, 6].Value = countlonhon50;
                    ws.Cells[i, 7].Value = countnhohon50;
                    total += counttotal;
                    totallonhon100 += countlonhon100;
                    totalbang100 += countbang100;
                    totallonhon50 += countlonhon50;
                    totalnhohon50 += countnhohon50;
                    i += 1;
                    stt += 1;
                }
                ws.Cells[i, 2].Value = "Tổng";
                ws.Cells[i, 3].Value = total;
                ws.Cells[i, 4].Value = totallonhon100;
                ws.Cells[i, 5].Value = totalbang100;
                ws.Cells[i, 6].Value = totallonhon50;
                ws.Cells[i, 7].Value = totalnhohon50;
                ws.Cells[7, 1, i, 7].Style.Border.Left.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 7].Style.Border.Top.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 7].Style.Border.Right.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 7].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                ws.Cells[7, 1, i, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[7, 1, i, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells[7, 3, i, 7].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[7, 3, i, 7].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells[7, 1, i, 7].Style.Font.Name = "Times New Roman";
                ws.Cells[i, 1, i, 7].Style.Font.Bold = true;

                return p;
            }
            catch (Exception ex)
            {
                throw;
            }
            //}
            return returnPackage;

        }

        #endregion

        #region KET XUAT EXCEL PDF KE HOACH THUC HIEN
        //ExportData
        public FileResult ExportData(int namKHId, int nhomDonViId, int donViId, string namKH)
        {
            ExcelPackage ep = this.getExcelPackKeHoachThucHien(namKHId, nhomDonViId, donViId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "TinhHinhThucHienKeHoach" + namKH + ".xlsx");

            return result;
        }

        //ExportMultiSheet
        public FileResult ExportBCTHMultiSheet(int namKHId, int nhomDonViId, int donViId, string namKH)
        {
            ExcelPackage ep = this.getExcelPackKeHoachThucHienMultiSheet(namKHId, nhomDonViId, donViId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "TinhHinhThucHienKeHoachAll" + namKH + ".xlsx");

            return result;
        }

        public FileResult PrintBCTHMultiSheet(int namKHId, int nhomDonViId, int donViId, string namKH)
        {
            ExcelPackage p = this.getExcelPackKeHoachThucHienMultiSheet(namKHId, nhomDonViId, donViId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            //phunx1, 20160422: xu ly open pdf
            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "BaoCaoTuan");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;
        }

        private bool HasData(List<ExportExcell> dataDonVi)
        {
            double sum = 0;
            for (int i = 0; i < dataDonVi.Count; i++)
            {
                sum += dataDonVi[i].GIA_TRI;
            }

            return sum > 0 ? true : false;
        }

        public FileResult OpenPdfData(int namKHId, int nhomDonViId, string namKH, int donViId)
        {
            ExcelPackage excelPackage = this.getExcelPackKeHoachThucHien(namKHId, nhomDonViId, donViId, namKH);
            MemoryStream inStream = new MemoryStream(excelPackage.GetAsByteArray());
            //phunx1, 20160422: xu ly open pdf
            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A3;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);

            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;

                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "BaoCaoTuan");
                    pdf.EndExport();
                    ms.Position = 0;
                    //return File(ms.ToArray(), "application/pdf");
                    return File(ms.ToArray(), "application/pdf");
                }
            }

            //inStream.Close();
            inStream.Dispose();
            excelPackage.Dispose();
            return null;
        }

        private ExcelPackage getExcelPackKeHoachThucHien(int namKHId, int nhomDonViId, int donViId, string namKH)
        {
            try
            {
                string tenNhomDV = string.Empty;
                if (nhomDonViId > 0)
                {
                    NhomDonViBO ndvObj = new NhomDonViBO();
                    NhomDonVi obj = ndvObj.GetById(nhomDonViId);
                    if (obj != null) tenNhomDV = "NHÓM " + obj.TenNhomDonVi.ToUpper();
                }

                ExportExcellBO sqlBO = new ExportExcellBO();
                int laChiTieuChinhThuc = 31;
                int laChiTieuDieuChinh = 61;
                List<DonVi> listDonVi = new List<DonVi>();
                if (donViId > 0)
                {
                    listDonVi.Add(donvibo.GetById(donViId));
                }
                else listDonVi = donvibo.GetAllDonViByNhom(nhomDonViId);

                List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, nhomDonViId);
                var keHoachThucHienDHQG = sqlBO.GetListKeHoachThucHienDHQG_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, nhomDonViId);
                var keHoachDieuChinhDHQG = sqlBO.GetListChiTieuDHQG_TheoNhom(namKHId, laChiTieuDieuChinh, donViId, nhomDonViId);

                List<List<ExportExcell>> dsChiTieuDonVi = new List<List<ExportExcell>>();
                List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
                List<ExportExcell> dataDonVi;

                ExportExcellBO exBO = new ExportExcellBO();
                bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);


                for (int i = 0; i < listDonVi.Count; i++)
                {
                    if (coKeHoachDC)
                    {
                        dataDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, listDonVi[i].DonViId, nhomDonViId);
                    }
                    else
                    {
                        dataDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, listDonVi[i].DonViId, nhomDonViId);
                    }
                    dsChiTieuDonVi.Add(dataDonVi);
                    dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, listDonVi[i].DonViId, nhomDonViId));
                }

                Color backGroundColor = Color.FromArgb(208, 206, 206);

                var excelPackage = new ExcelPackage();
                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);

                #region Set Margion
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;
                sheet.PrinterSettings.FitToWidth = 1;
                #endregion

                #region Set column width and set text alignment
                sheet.Column(2).Width = dvtColumnWidth;
                for (int i = 3; i < listDonVi.Count * 3 + 8; i++)
                {
                    sheet.Column(i).Width = dvColumnWidth;
                    sheet.Column(i).Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                }
                #endregion

                var printArea = sheet.Cells[1, 1, 1000, listDonVi.Count * 3 + 7];
                printArea.Style.Font.SetFromFont(new Font("Arial", 10));

                // output a line for the headers
                sheet.Name = "Tình hình thực hiện KH " + namKHId;
                int startRowLogo = 1;
                int startColumnLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                startRowLogo += 1;
                if (donViId > 0)
                {
                    donViCell = sheet.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo];
                    donViCell.Merge = true;
                    donViCell.Value = listDonVi.Count > 0 ? listDonVi[0].TenDonVi.ToUpper() : string.Empty;
                    donViCell.Style.Font.Bold = true;
                    donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                }
                else
                {
                    donViCell = sheet.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo];
                    donViCell.Merge = true;
                    donViCell.Value = tenNhomDV;
                    donViCell.Style.Font.Bold = true;
                    donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                }

                int startRowTile = startRowLogo + 1;
                var titleCell = sheet.Cells[startRowTile, 2, startRowTile, listDonVi.Count * 3 + 7];
                titleCell.Merge = true;
                titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Arial", 12));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Thong tin moc kiem soat
                int namkh = 0;
                if (!string.IsNullOrEmpty(namKH))
                {
                    try
                    {
                        namkh = System.Convert.ToInt32(namKH);
                    }
                    catch { }
                }
                MocKiemSoatBO mc = new MocKiemSoatBO();
                MocKiemSoat mks = mc.GetMocKSIsActive(namkh);

                string kyBC = string.Empty;
                if (mks != null)
                {
                    kyBC = mks.MOC_KIEM_SOAT;
                }
                startRowTile += 1;
                var kyBaoCaoCell = sheet.Cells[startRowTile, 2, startRowTile, listDonVi.Count * 3 + 7];
                kyBaoCaoCell.Merge = true;
                kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
                kyBaoCaoCell.Style.Font.Bold = true;
                kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                startRow = startRowTile + 1;
                var cellRange1 = sheet.Cells[startRow, startCol, startRow + 1, startCol];
                cellRange1.Merge = true;
                cellRange1.Value = "Chỉ tiêu";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                //ExcelColumn colChiTieu = sheet.Column(startCol);
                //colChiTieu.Style.WrapText = true;
                //colChiTieu.Width = 60;

                #region Set width of column ChiTieu
                sheet.Column(startCol).Style.WrapText = true;
                double chitieuWidthMin = 30;
                double cacDonViWidth = (listDonVi.Count * 3 + 5) * dvColumnWidth + dvtColumnWidth;
                double chitieuWidth = portraitSize - cacDonViWidth;
                if (chitieuWidth < chitieuWidthMin)
                {
                    chitieuWidth = Math.Max(chitieuWidthMin, landscapeSize - cacDonViWidth);
                    isLandScape = true;
                }
                sheet.Column(startCol).Width = chitieuWidth;
                #endregion

                cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                ExcelColumn colDonVi = sheet.Column(startCol + 1);
                colDonVi.Style.WrapText = true;
                colDonVi.Width = 15;

                var cellRange2 = sheet.Cells[startRow, startCol + 2, startRow, startCol + 6];
                cellRange2.Merge = true;
                cellRange2.Value = "ĐHQGHN";

                cellRange2 = sheet.Cells[startRow + 1, startCol + 2];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "KH";
                cellRange2 = sheet.Cells[startRow + 1, startCol + 3];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%";
                cellRange2 = sheet.Cells[startRow + 1, startCol + 4];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "ĐC";
                cellRange2 = sheet.Cells[startRow + 1, startCol + 5];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%";
                cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "TTH";

                int colStart = startCol + 7;
                for (int i = 0; i < listDonVi.Count; i++)
                {
                    cellRange2 = sheet.Cells[startRow, colStart, startRow, colStart + 2];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = listDonVi[i].TenVietTat;

                    cellRange2 = sheet.Cells[startRow + 1, colStart];
                    cellRange2.Value = "KH";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    cellRange2 = sheet.Cells[startRow + 1, colStart + 1];
                    cellRange2.Value = "TH";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    cellRange2 = sheet.Cells[startRow + 1, colStart + 2];
                    cellRange2.Value = "%";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    colStart = colStart + 3;
                }
                cellRange2 = sheet.Cells[startRow, startCol + 2, startRow + 1, colStart - 1];
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                int beginRow = startRow + 1;
                int beginCol = startCol;
                for (int i = 0; i < chiTieuChinhThucDHQG.Count; i++)
                {
                    beginRow++;
                    cellRange2 = sheet.Cells[beginRow, startCol];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    if (chiTieuChinhThucDHQG[i].CAP == 1)
                    {
                        cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU.ToUpper();
                    }
                    else cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;

                    if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                        chiTieuChinhThucDHQG[i].CAP == 2)
                    {
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //%
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //DC
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //%
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }

                    if (chiTieuChinhThucDHQG[i].CAP > 2)
                    {
                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //%
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                            Math.Round(((float)keHoachThucHienDHQG[i].GIA_TRI / chiTieuChinhThucDHQG[i].GIA_TRI) * 100) : 0;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //DC
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Value = keHoachDieuChinhDHQG[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //%
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                            Math.Round(((float)keHoachDieuChinhDHQG[i].GIA_TRI / chiTieuChinhThucDHQG[i].GIA_TRI) * 100) : 0;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ? keHoachThucHienDHQG[i].GIA_TRI : 0;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    }
                }
                cellRange2 = sheet.Cells[startRow + 1, startCol, beginRow, startCol + 1];
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;

                colStart = startCol + 7;
                beginRow = startRow + 1;
                for (int i = 0; i < dsChiTieuDonVi.Count; i++)
                {
                    int len = dsChiTieuDonVi[i].Count;
                    beginRow = startRow + 1;
                    for (int j = 0; j < len; j++)
                    {
                        beginRow++;
                        cellRange2 = sheet.Cells[beginRow, colStart];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        if (dsChiTieuDonVi[i][j].CAP > 2)
                        {
                            cellRange2 = sheet.Cells[beginRow, colStart];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cellRange2.Value = dsChiTieuDonVi[i][j].GIA_TRI;
                            cellRange2 = sheet.Cells[beginRow, colStart + 1];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = dsKHThucHienDonVi[i][j].GIA_TRI;

                            cellRange2 = sheet.Cells[beginRow, colStart + 2];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = dsChiTieuDonVi[i][j].GIA_TRI > 0 ?
                                Math.Round(((float)dsKHThucHienDonVi[i][j].GIA_TRI / dsChiTieuDonVi[i][j].GIA_TRI) * 100) : 0;
                        }
                        else
                        {
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cellRange2 = sheet.Cells[beginRow, colStart + 1];
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cellRange2 = sheet.Cells[beginRow, colStart + 2];
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        }
                    }
                    colStart = colStart + 3;
                }

                return excelPackage;
            }
            catch (Exception ex)
            {

            }
            return null;
        }

        private ExcelPackage getExcelPackKeHoachThucHienMultiSheet(int namKHId, int nhomDonViId, int donViId, string namKH)
        {
            try
            {
                #region Sheet DHQG
                string tenNhomDV = string.Empty;
                ExportExcellBO sqlBO = new ExportExcellBO();
                int laChiTieuChinhThuc = 31;
                int laChiTieuDieuChinh = 61;
                List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuChinhThuc);
                var keHoachThucHienDHQG = sqlBO.GetListKeHoachThucHienDHQG(namKHId, laChiTieuChinhThuc);
                var keHoachDieuChinhDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuDieuChinh);

                ExportExcellBO exBO = new ExportExcellBO();
                bool hasDieuChinh = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

                //hasDieuChinh = true; //Test du lieu co dieu chinh

                List<DonVi> listDonVi = donvibo.GetAll();
                List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
                List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach(namKHId, 0);

                for (int i = 0; i < listDonVi.Count; i++)
                {
                    dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi(namKHId, laChiTieuChinhThuc, listDonVi[i].DonViId));
                }
                List<double> tongThucHien = TongThucHien(namKHId);

                Color backGroundColor = Color.FromArgb(208, 206, 206);

                ExcelPackage excelPackage = new ExcelPackage();
                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";

                var sheet = excelPackage.Workbook.Worksheets.Add("DHQG");
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, 1000, listDonVi.Count * 3 + 7];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                #region Title
                // output a line for the headers
                sheet.Name = "Tình hình thực hiện KH " + namKHId;
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                if (hasDieuChinh) titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];

                titleCell.Merge = true;
                titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Thong tin moc kiem soat
                int namkh = 0;
                if (!string.IsNullOrEmpty(namKH))
                {
                    try
                    {
                        namkh = System.Convert.ToInt32(namKH);
                    }
                    catch { }
                }
                MocKiemSoatBO mc = new MocKiemSoatBO();
                MocKiemSoat mks = mc.GetMocKSIsActive(namkh);

                string kyBC = string.Empty;
                if (mks != null)
                {
                    kyBC = mks.MOC_KIEM_SOAT;
                }
                startRowTile += 1;
                var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 7];
                if (hasDieuChinh) kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 9];
                kyBaoCaoCell.Merge = true;
                kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
                kyBaoCaoCell.Style.Font.Bold = true;
                kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 4;
                sheet.Column(2).Width = 5;
                if (hasDieuChinh) sheet.Column(3).Width = 28;
                else sheet.Column(3).Width = 44;
                sheet.Column(4).Width = 10;
                sheet.Column(5).Width = 10;
                sheet.Column(6).Width = 8;
                sheet.Column(7).Width = 8;
                sheet.Column(8).Width = 8;
                sheet.Column(9).Width = 8;
                sheet.Column(10).Width = 8;
                #endregion

                startRow = startRowTile + 1;

                #region Row header
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol + 1];
                cellRange0.Merge = true;
                cellRange0.Value = "STT";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;

                //Chitieu
                var cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "Chỉ tiêu";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;

                //Don vi giam sat
                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
                cellRange1.Merge = true;
                cellRange1.Value = "ĐV phân bổ và giám sát";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                var cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 7];
                if (hasDieuChinh) cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 9];

                cellRange2.Merge = true;
                cellRange2.Value = "ĐHQGHN";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 5];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "KH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                if (hasDieuChinh)
                {
                    cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = "ĐC";
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = "TTH";
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    cellRange2 = sheet.Cells[startRow + 1, startCol + 8];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = "%(KH)";
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    cellRange2 = sheet.Cells[startRow + 1, startCol + 9];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = "%(ĐC)";
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
                else
                {
                    cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = "TTH";
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = "%";
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
                #endregion

                int beginRow = startRow + 1;
                int beginCol = startCol;
                int sttLevel1 = 1;
                int sttLevel2 = 1;

                #region Values
                for (int i = 0; i < chiTieuChinhThucDHQG.Count; i++)
                {
                    beginRow++;

                    //if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                    //    chiTieuChinhThucDHQG[i].CAP == 2)
                    if (chiTieuChinhThucDHQG[i].CAP == 1)
                    {
                        cellRange2 = sheet.Cells[beginRow, startCol, beginRow, startCol + 4];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU.ToUpper();

                        if (hasDieuChinh)
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //DC
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 8];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //%(DC)
                            cellRange2 = sheet.Cells[beginRow, startCol + 9];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                    }
                    else if (chiTieuChinhThucDHQG[i].CAP == 2)
                    {
                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if (hasDieuChinh)
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //DC
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 8];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //%(DC)
                            cellRange2 = sheet.Cells[beginRow, startCol + 9];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        }
                        else
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        }

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                    else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                    {
                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        //cellRange2.Value = "Đơn vị giám sát";
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;
                        //if (i < donViPhuTrach.Count)
                        //{
                        //    if (donViPhuTrach[i].CAP == 2)
                        //    {
                        //        int chiTieuCap3Index = i + 1;
                        //        if (chiTieuCap3Index < donViPhuTrach.Count)
                        //            cellRange2.Value = (donViPhuTrach[chiTieuCap3Index].DON_VI ?? string.Empty);
                        //    }
                        //}
                        if (i < donViPhuTrach.Count) cellRange2.Value = donViPhuTrach[i].DON_VI ?? string.Empty;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        if (hasDieuChinh)
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Numberformat.Format = "###,###,###";

                            //DC
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Value = keHoachDieuChinhDHQG[i].GIA_TRI;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Numberformat.Format = "###,###,###";

                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Value = tongThucHien[i];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Numberformat.Format = "###,###,###";

                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 8];
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                                Math.Round(((float)keHoachThucHienDHQG[i].GIA_TRI / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, 2) : 0;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            //%(DC)
                            cellRange2 = sheet.Cells[beginRow, startCol + 9];
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                                Math.Round(((float)keHoachDieuChinhDHQG[i].GIA_TRI / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, 2) : 0;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            sttLevel2 = sttLevel2 + 1;
                        }
                        else
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Numberformat.Format = "###,###,###";

                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Value = tongThucHien[i];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Numberformat.Format = "###,###,###";

                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                                Math.Round(((float)keHoachThucHienDHQG[i].GIA_TRI / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, 2) : 0;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###.##";

                            sttLevel2 = sttLevel2 + 1;
                        }
                    }
                }
                #endregion
                #endregion


                #region Cac sheet cua tung don vi

                List<DonVi> lstDonVi = new List<DonVi>();
                if (donViId > 0)
                {
                    lstDonVi.Add(donvibo.GetById(donViId));
                }
                else lstDonVi = donvibo.GetAllDonViByNhom(nhomDonViId);

                foreach (DonVi donVi in lstDonVi)
                {
                    var sheetdv = excelPackage.Workbook.Worksheets.Add(donVi.TenVietTat);
                    List<ExportExcell> chiTieuChinhThuc;

                    if (hasDieuChinh)
                    {
                        chiTieuChinhThuc = sqlBO.GetListChiTieuDHQG_TheoNhom(namKHId, laChiTieuDieuChinh, donVi.DonViId, 0);
                    }
                    else
                    {
                        chiTieuChinhThuc = sqlBO.GetListChiTieuDHQG_TheoNhom(namKHId, laChiTieuChinhThuc, donVi.DonViId, 0);
                    }

                    List<ExportExcell> chiTieuDonVi;
                    if (hasDieuChinh)
                    {
                        chiTieuDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, donVi.DonViId, 0);
                    }
                    else
                    {
                        chiTieuDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donVi.DonViId, 0);
                    }
                    List<ExportExcell> keHoachThucHienDonVi = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donVi.DonViId, 0);

                    List<ExportExcell> banPhuTrach = sqlBO.GetListBanPhuTrach_TheoDonVi(namKHId, 0, donViId);

                    List<ExportExcell> chiTieuRiengDauNam = sqlBO.GetListChiTieuRiengDonVi_KeHoach(namKHId, donVi.DonViId, 21);
                    List<ExportExcell> chiTieuRiengThucHien = sqlBO.GetListChiTieuRiengDonVi_KeHoachThucHien(namKHId, donVi.DonViId);

                    #region Set Margion
                    sheetdv.PrinterSettings.TopMargin = topMargin;
                    sheetdv.PrinterSettings.BottomMargin = bottomMargin;
                    sheetdv.PrinterSettings.LeftMargin = leftMargin;
                    sheetdv.PrinterSettings.RightMargin = rightMargin;
                    sheetdv.PrinterSettings.FitToWidth = 1;
                    #endregion

                    #region Set Column Width
                    sheetdv.Column(1).Width = 5;
                    sheetdv.Column(2).Width = 42;
                    sheetdv.Column(3).Width = 10;
                    sheetdv.Column(4).Width = 10;
                    sheetdv.Column(5).Width = 8;
                    sheetdv.Column(6).Width = 8;
                    sheetdv.Column(7).Width = 8;
                    sheetdv.Column(8).Width = 8;
                    #endregion

                    printArea = sheetdv.Cells[1, 1, 1000, 10];
                    printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                    //// output a line for the headers
                    //sheetdv.Name = "Chỉ tiêu " + namKH;
                    startCol = 1;
                    startRowLogo = 1;
                    int startColumnLogo = 1;
                    donViCell = sheetdv.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo + 5];
                    donViCell.Merge = true;
                    donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                    donViCell.Style.Font.Bold = true;
                    donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                    donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    startRowLogo += 1;

                    startRowTile = startRowLogo + 1;
                    titleCell = sheetdv.Cells[startRowTile, 1, startRowTile, 7];
                    titleCell.Merge = true;
                    titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                    titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                    titleCell.Style.Font.Bold = true;
                    titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    #region Thong tin moc kiem soat
                    namkh = 0;
                    if (!string.IsNullOrEmpty(namKH))
                    {
                        try
                        {
                            namkh = System.Convert.ToInt32(namKH);
                        }
                        catch { }
                    }

                    kyBC = string.Empty;
                    if (mks != null)
                    {
                        kyBC = mks.MOC_KIEM_SOAT;
                    }
                    startRowTile += 1;
                    kyBaoCaoCell = sheetdv.Cells[startRowTile, 1, startRowTile, 7];
                    kyBaoCaoCell.Merge = true;
                    kyBaoCaoCell.Value = listDonVi != null ? donVi.TenDonVi.ToUpper() : string.Empty;// "Kỳ báo cáo: " + kyBC;
                    kyBaoCaoCell.Style.Font.Bold = true;
                    kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    #endregion

                    startRow = startRowTile + 1;
                    cellRange1 = sheetdv.Cells[startRow, 1, startRow + 1, 1];
                    cellRange1.Merge = true;
                    cellRange1.Value = "A";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    ExcelColumn colNhomCT = sheetdv.Column(1);
                    colNhomCT.Style.WrapText = true;

                    cellRange1 = sheetdv.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Các Chỉ tiêu Chung".ToUpper();
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    ExcelColumn colChiTieu = sheetdv.Column(startCol);
                    colChiTieu.Style.WrapText = true;

                    #region Header
                    cellRange1 = sheetdv.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                    cellRange1.Merge = true;
                    cellRange1.Value = "ĐV phân bổ và giám sát";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheetdv.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Đơn vị tính";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheetdv.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Mức chỉ tiêu năm " + namKH + " của toàn ĐHQGHN";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    // Mức chỉ tiêu năm 2016 của đơn vị 
                    cellRange1 = sheetdv.Cells[startRow, startCol + 5, startRow + 1, startCol + 5];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Mức chỉ tiêu năm " + namKH + " của đơn vị";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheetdv.Cells[startRow, startCol + 6, startRow, startCol + 7];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Kết quả thực hiện";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheetdv.Cells[startRow + 1, startCol + 6];
                    cellRange1.Value = "Thực hiện " + kyBC;
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheetdv.Cells[startRow + 1, startCol + 7];
                    cellRange1.Value = "Tỉ lệ hoàn thành";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;
                    #endregion

                    #region Chi tieu chung
                    startCol = 2;
                    beginRow = startRow + 1;
                    beginCol = startCol;

                    ExcelRange banPhuTrachCell = null;
                    ExcelRange cellRange = null;
                    for (int i = 0; i < chiTieuChinhThuc.Count; i++)
                    {
                        beginRow++;

                        var cellSTT = sheetdv.Cells[beginRow, 1];
                        cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellSTT.Value = (i + 1).ToString();

                        cellRange = sheetdv.Cells[beginRow, startCol + 3];
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        banPhuTrachCell = sheetdv.Cells[beginRow, startCol + 1];
                        banPhuTrachCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        banPhuTrachCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        banPhuTrachCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                            chiTieuChinhThucDHQG[i].CAP == 2)
                        {
                            cellRange.Style.Font.Bold = true;
                            cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            banPhuTrachCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            banPhuTrachCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            if (i < banPhuTrach.Count)
                            {
                                banPhuTrachCell.Value = banPhuTrach[i].DON_VI;
                                if (banPhuTrach[i].CAP == 2)
                                {
                                    int chiTieuCap3 = i + 1;
                                    if (chiTieuCap3 < banPhuTrach.Count)
                                    {
                                        banPhuTrachCell.Value = banPhuTrach[chiTieuCap3].DON_VI;
                                    }
                                }
                            }

                        }

                        if (chiTieuChinhThucDHQG[i].CAP > 2)
                        {
                            cellRange.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                            //banPhuTrachCell.Value = banPhuTrach[i].DON_VI;
                        }
                    }
                    cellRange = sheetdv.Cells[startRow + 1, startCol, beginRow, startCol + 1];
                    cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    ExcelColumn colDonViPhuTrach = sheetdv.Column(startCol + 1);
                    colDonViPhuTrach.Style.WrapText = true;
                    //colChiTieu.Width = 50;

                    int colStart = startCol;
                    beginRow = startRow + 1;

                    int len = chiTieuDonVi.Count;
                    beginRow = startRow + 1;
                    ExcelRange celDonViTinh = null;
                    for (int j = 0; j < len; j++)
                    {
                        beginRow++;
                        cellRange = sheetdv.Cells[beginRow, startCol];
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        celDonViTinh = sheetdv.Cells[beginRow, startCol + 2];
                        cellRange.Value = chiTieuDonVi[j].TEN_CHI_TIEU;
                        cellRange.Style.WrapText = true;
                        celDonViTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        if (chiTieuDonVi[j].CAP <= 2)
                        {
                            //Don vi tinh
                            celDonViTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            celDonViTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            celDonViTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            celDonViTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            //Ten chi tieu
                            cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            //cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange.Style.WrapText = true;

                            cellRange = sheetdv.Cells[beginRow, colStart + 4];
                            cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            //KH
                            cellRange = sheetdv.Cells[beginRow, colStart + 5];
                            cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cellRange = sheetdv.Cells[beginRow, colStart + 6];
                            cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        }
                        else if (chiTieuDonVi[j].CAP > 2)
                        {
                            //cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            celDonViTinh.Value = chiTieuDonVi[j].DON_VI_TINH;
                            celDonViTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            celDonViTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            celDonViTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            //Chỉ tiêu đơn vị
                            cellRange = sheetdv.Cells[beginRow, colStart + 4];
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange.Value = chiTieuDonVi[j].GIA_TRI;
                            cellRange.Style.Numberformat.Format = "###,###,###";

                            //Thực hiện 
                            cellRange = sheetdv.Cells[beginRow, colStart + 5];
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange.Value = keHoachThucHienDonVi[j].GIA_TRI;
                            cellRange.Style.Numberformat.Format = "###,###,###";

                            //Tỉ lệ hoàn thành
                            cellRange = sheetdv.Cells[beginRow, colStart + 6];
                            cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange.Value = chiTieuDonVi[j].GIA_TRI > 0 ?
                                Math.Round(((float)keHoachThucHienDonVi[j].GIA_TRI / chiTieuDonVi[j].GIA_TRI) * 100, 2) : 0;
                        }
                    }
                    #endregion

                    int startRowChiTieuRieng = beginRow + 1;

                    #region Chi tieu rieng
                    //startCol = 2;
                    //if (chiTieuRiengDauNam.Count > 0)
                    //{
                    //    try
                    //    {
                    //        var cellNhomCTR = sheetdv.Cells[startRowChiTieuRieng, 1, startRowChiTieuRieng, 1];
                    //        cellNhomCTR.Merge = true;
                    //        cellNhomCTR.Value = "B";
                    //        cellNhomCTR.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    //        cellNhomCTR.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //        cellNhomCTR.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //        cellNhomCTR.Style.Font.Bold = true;
                    //        ExcelColumn colNhomCTR = sheetdv.Column(1);
                    //        colNhomCTR.Style.WrapText = true;

                    //        ExcelRange cotChiTieu = sheetdv.Cells[startRowChiTieuRieng, startCol];
                    //        ExcelRange cotDonviTinh = sheetdv.Cells[startRowChiTieuRieng, startCol + 1, startRowChiTieuRieng, startCol + 2];
                    //        ExcelRange cotGiaTriChiTieu = sheetdv.Cells[startRowChiTieuRieng, startCol + 3, startRowChiTieuRieng, startCol + 4];
                    //        ExcelRange cotThucHien = sheetdv.Cells[startRowChiTieuRieng, startCol + 5];
                    //        ExcelRange cotTiLe = sheetdv.Cells[startRowChiTieuRieng, startCol + 6];

                    //        cotChiTieu.Value = "CÁC CHỈ TIÊU ĐẶC THÙ CỦA ĐƠN VỊ";
                    //        cotChiTieu.Style.Font.Bold = true;
                    //        cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //        cotChiTieu.Style.WrapText = true;
                    //        cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //        cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //        cotDonviTinh.Merge = true;
                    //        cotDonviTinh.Value = "Đơn vị tính";
                    //        cotDonviTinh.Style.Font.Bold = true;
                    //        cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //        cotDonviTinh.Style.WrapText = true;
                    //        cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //        cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //        cotGiaTriChiTieu.Merge = true;
                    //        cotGiaTriChiTieu.Value = "Mức chỉ tiêu năm " + namKH + " đơn vị đăng ký";
                    //        cotGiaTriChiTieu.Style.Font.Bold = true;
                    //        cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //        cotGiaTriChiTieu.Style.WrapText = true;
                    //        cotGiaTriChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //        cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //        cotThucHien.Style.Font.Bold = true;
                    //        cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //        cotThucHien.Style.WrapText = true;

                    //        cotTiLe.Style.Font.Bold = true;
                    //        cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //        cotTiLe.Style.WrapText = true;
                    //        beginRow = startRowChiTieuRieng;

                    //        for (int i = 0; i < chiTieuRiengDauNam.Count; i++)
                    //        {
                    //            beginRow++;
                    //            var cellSTT = sheetdv.Cells[beginRow, 1];
                    //            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    //            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //            cellSTT.Value = (i + 1).ToString();

                    //            cotChiTieu = sheetdv.Cells[beginRow, startCol];
                    //            cotDonviTinh = sheetdv.Cells[beginRow, startCol + 1, beginRow, startCol + 2];
                    //            cotGiaTriChiTieu = sheetdv.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                    //            cotThucHien = sheetdv.Cells[beginRow, startCol + 5];
                    //            cotTiLe = sheetdv.Cells[beginRow, startCol + 6];

                    //            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                    //            cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //            cotChiTieu.Style.WrapText = true;
                    //            cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //            cotDonviTinh.Merge = true;
                    //            cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //            cotDonviTinh.Style.WrapText = true;
                    //            cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    //            cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    //            cotGiaTriChiTieu.Merge = true;
                    //            cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //            cotGiaTriChiTieu.Style.WrapText = true;
                    //            cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //            cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //            cotThucHien.Style.WrapText = true;
                    //            cotThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //            cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //            cotTiLe.Style.WrapText = true;
                    //            cotTiLe.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //            if (chiTieuRiengDauNam[i].CAP <= 2)
                    //            {
                    //                cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //                cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //                cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //                cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //                cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //                cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //                cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //                cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //                cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //                cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //            }
                    //            else
                    //            {
                    //                //cotDonviTinh.Value = (chiTieuRiengDauNam[i].DON_VI_TINH.ToUpper() == "NA" ?
                    //                //    string.Empty : chiTieuRiengDauNam[i].DON_VI_TINH);
                    //                cotDonviTinh.Value = chiTieuRiengDauNam[i].DON_VI_TINH;
                    //                cotGiaTriChiTieu.Value = chiTieuRiengDauNam[i].GIA_TRI;
                    //                cotThucHien.Value = chiTieuRiengThucHien[i].GIA_TRI;
                    //                cotTiLe.Value = (chiTieuRiengDauNam[i].GIA_TRI > 0 ?
                    //                    (chiTieuRiengThucHien[i].GIA_TRI / chiTieuRiengDauNam[i].GIA_TRI) * 100 : 0);
                    //            }
                    //        }
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        return null;
                    //    }
                    //}
                    #endregion
                }

                #endregion


                return excelPackage;
            }
            catch
            {
                return null;
            }
        }

        #endregion

        #region Bao cao tien do thuc hien cua DHQG
        public FileResult ExportDataDHQG(int namKHId, string namKH, int mocKS)
        {
            ExcelPackage excelPackage = getExcelExportDataDHQG(namKHId, namKH, mocKS); //getExcelExportDataDonVi(namKHId, namKH, 55);  //
            FileResult result = File(excelPackage.GetAsByteArray(),
                "application/octet-stream", "TinhHinhThucHienKeHoachDHQG" + namKH + ".xlsx");
            excelPackage.Dispose();

            return result;

        }

        public FileResult PrintDataDHQG(int namKHId, string namKH, int mocKS)
        {
            ExcelPackage p = getExcelExportDataDHQG(namKHId, namKH, mocKS);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "KeHoachThucHienDHQG");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        public ExcelPackage getExcelExportDataDHQG(int namKHId, string namKH, int mocKS)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            //chi tieu dinh huong
            //List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuChinhThuc);
            List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetChiTieuDinhHuong(namKHId, laChiTieuChinhThuc);
            var keHoachThucHienDHQG = sqlBO.GetListKeHoachThucHienDHQG(namKHId, laChiTieuChinhThuc);
            var keHoachDieuChinhDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuDieuChinh);
            //bool hasDieuChinh = HasData(keHoachDieuChinhDHQG);
            bool hasDieuChinh = sqlBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

            //BinhQuanChiTieu(chiTieuChinhThucDHQG, namKHId);
            //BinhQuanChiTieu(keHoachThucHienDHQG, namKHId);
            if (hasDieuChinh)
            {
                BinhQuanChiTieu(keHoachDieuChinhDHQG, namKHId);
            }

            //hasDieuChinh = true; //Test du lieu co dieu chinh

            List<DonVi> listDonVi = donvibo.GetAll();
            //List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
            List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach(namKHId, 0);

            //for (int i = 0; i < listDonVi.Count; i++)
            //{
            //    dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi(namKHId, laChiTieuChinhThuc, listDonVi[i].DonViId));
            //}
            List<double> tongThucHien = TongThucHien(namKHId, mocKS);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            //using (var excelPackage = new ExcelPackage())
            //{
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, 1000, listDonVi.Count * 3 + 7];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
            if (hasDieuChinh) titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];

            titleCell.Merge = true;
            titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin moc kiem soat
            int namkh = 0;
            if (!string.IsNullOrEmpty(namKH))
            {
                try
                {
                    namkh = System.Convert.ToInt32(namKH);
                }
                catch { }
            }
            MocKiemSoatBO mc = new MocKiemSoatBO();
            MocKiemSoat mks = mc.GetbyID(mocKS);// mc.GetMocKSIsActive(namkh);

            string kyBC = string.Empty;
            if (mks != null)
            {
                kyBC = mks.MOC_KIEM_SOAT;
            }
            startRowTile += 1;
            var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
            if (hasDieuChinh) kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 9];
            kyBaoCaoCell.Merge = true;
            kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
            kyBaoCaoCell.Style.Font.Bold = true;
            kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            if (hasDieuChinh) sheet.Column(3).Width = 28;
            else sheet.Column(3).Width = 44;
            sheet.Column(4).Width = 10;
            sheet.Column(5).Width = 10;
            sheet.Column(6).Width = 8;
            sheet.Column(7).Width = 8;
            sheet.Column(8).Width = 8;
            sheet.Column(9).Width = 8;
            sheet.Column(10).Width = 8;
            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;


            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            var cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 7];
            if (hasDieuChinh) cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 9];

            cellRange2.Merge = true;
            cellRange2.Value = "ĐHQGHN";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            cellRange2 = sheet.Cells[startRow + 1, startCol + 5];
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Value = "KH";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            if (hasDieuChinh)
            {
                cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "ĐC";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "TTH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 8];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%(KH)";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 9];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%(ĐC)";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            }
            else
            {
                cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "TTH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            }


            int beginRow = startRow + 1;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            bool cungDonViTinh = false;
            string currDonViTinh = "";
            double tongKH = 0;
            double tongTH = 0;
            double tongDC = 0;
            //double tyLeDC = 0;
            string dvPhuTrachCTCap2 = "";
            int rowIndexCTCap2 = 0;
            for (int i = 0; i < chiTieuChinhThucDHQG.Count; i++)
            {
                //if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                //    chiTieuChinhThucDHQG[i].CAP == 2)
                if (chiTieuChinhThucDHQG[i].CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    cellRange2 = sheet.Cells[beginRow, startCol, beginRow, startCol + 4];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU.ToUpper();

                    if (hasDieuChinh)
                    {
                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //DC
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 8];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //%(DC)
                        cellRange2 = sheet.Cells[beginRow, startCol + 9];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    else
                    {
                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    #endregion
                }
                else if (chiTieuChinhThucDHQG[i].CAP == 2)
                {
                    if (chiTieuChinhThucDHQG[i].LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        #region In Tong chi tieu (neu co)
                        if (hasDieuChinh)
                        {
                            if ((cungDonViTinh) && (currDonViTinh != "%"))
                            {
                                cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                                cellRange2.Value = dvPhuTrachCTCap2;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                                cellRange2.Value = currDonViTinh;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                                cellRange2.Value = tongKH;
                                if ((tongKH < 1) && (tongKH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongKH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                                cellRange2.Value = tongDC;

                                if ((tongDC < 1) && (tongDC > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongDC % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                                cellRange2.Value = tongTH;

                                if ((tongTH < 1) && (tongTH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongTH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 9];
                                //cellRange2.Value = Math.Round((tongTH / tongKH) * 100, 2);
                                cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 10];
                                if (tongDC > 0) cellRange2.Value = Math.Round((tongTH / tongDC) * 100, MidpointRounding.AwayFromZero);
                                else cellRange2.Value = "";
                                cungDonViTinh = false;
                            }
                        }
                        else
                        {
                            if ((cungDonViTinh) && (currDonViTinh != "%"))
                            {
                                cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                                cellRange2.Value = dvPhuTrachCTCap2;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                                cellRange2.Value = currDonViTinh;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                                cellRange2.Value = tongKH;
                                if ((tongKH < 1) && (tongKH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongKH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                                cellRange2.Value = tongTH;

                                if ((tongTH < 1) && (tongTH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongTH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                                cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                                cungDonViTinh = false;
                            }
                        }
                        #endregion

                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        cungDonViTinh = true;
                        currDonViTinh = "";
                        tongKH = 0;
                        tongTH = 0;
                        tongDC = 0;

                        chiTieuAo = false;
                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if (hasDieuChinh)
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //DC
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 8];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //%(DC)
                            cellRange2 = sheet.Cells[beginRow, startCol + 9];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        }
                        else
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        }

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                }
                else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                {
                    beginRow++;
                    if (chiTieuAo)
                    {
                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        if (sttLevel2 == 1)
                        {
                            dvPhuTrachCTCap2 = donViPhuTrach[i].DON_VI;
                            currDonViTinh = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                            cungDonViTinh = true;
                            tongKH = tongKH + chiTieuChinhThucDHQG[i].GIA_TRI;
                            tongTH = tongTH + tongThucHien[i];
                            tongDC = tongDC + keHoachDieuChinhDHQG[i].GIA_TRI;
                        }
                        else
                        {
                            if (cungDonViTinh)
                            {
                                if ((currDonViTinh != null) && (chiTieuChinhThucDHQG[i].DON_VI_TINH != null) &&
                                    (currDonViTinh.Trim().ToUpper() == chiTieuChinhThucDHQG[i].DON_VI_TINH.Trim().ToUpper()))
                                {
                                    cungDonViTinh = true;
                                    tongKH = tongKH + chiTieuChinhThucDHQG[i].GIA_TRI;
                                    tongTH = tongTH + tongThucHien[i];
                                    tongDC = tongDC + keHoachDieuChinhDHQG[i].GIA_TRI;
                                }
                                else
                                {
                                    cungDonViTinh = false;
                                }
                            }

                            if (dvPhuTrachCTCap2 != donViPhuTrach[i].DON_VI) dvPhuTrachCTCap2 = "";
                        }

                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    cellRange2 = sheet.Cells[beginRow, startCol + 2];
                    cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //Don vi giam sat
                    cellRange2 = sheet.Cells[beginRow, startCol + 3];
                    //cellRange2.Value = "Đơn vị giám sát";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.WrapText = true;
                    //if (i < donViPhuTrach.Count)
                    //{
                    //    if (donViPhuTrach[i].CAP == 2)
                    //    {
                    //        int chiTieuCap3Index = i + 1;
                    //        if (chiTieuCap3Index < donViPhuTrach.Count)
                    //            cellRange2.Value = (donViPhuTrach[chiTieuCap3Index].DON_VI ?? string.Empty);
                    //    }
                    //}
                    if (i < donViPhuTrach.Count) cellRange2.Value = donViPhuTrach[i].DON_VI ?? string.Empty;

                    //Don vi tinh
                    cellRange2 = sheet.Cells[beginRow, startCol + 4];
                    cellRange2.Value = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange2.Style.WrapText = true;

                    if (hasDieuChinh)
                    {
                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((chiTieuChinhThucDHQG[i].GIA_TRI < 1) && (chiTieuChinhThucDHQG[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuChinhThucDHQG[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //DC
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        if (keHoachDieuChinhDHQG[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            cellRange2.Value = keHoachDieuChinhDHQG[i].GIA_TRI;
                        }
                        else cellRange2.Value = string.Empty;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((keHoachDieuChinhDHQG[i].GIA_TRI < 1) && (keHoachDieuChinhDHQG[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (keHoachDieuChinhDHQG[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Value = tongThucHien[i];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((tongThucHien[i] < 1) && (tongThucHien[i] > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (tongThucHien[i] % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 8];
                        if (keHoachDieuChinhDHQG[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                                //Math.Round(((float)tongThucHien[i] / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, 2) : 0;
                                Math.Round(((float)tongThucHien[i] / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                        }
                        else cellRange2.Value = string.Empty;

                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //%(DC)
                        cellRange2 = sheet.Cells[beginRow, startCol + 9];
                        if (keHoachDieuChinhDHQG[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            cellRange2.Value = keHoachDieuChinhDHQG[i].GIA_TRI > 0 ?
                                //Math.Round(((float)tongThucHien[i] / keHoachDieuChinhDHQG[i].GIA_TRI) * 100, 2) : 0;
                                Math.Round(((float)tongThucHien[i] / keHoachDieuChinhDHQG[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                        }
                        else cellRange2.Value = string.Empty;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    }
                    else
                    {
                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((chiTieuChinhThucDHQG[i].GIA_TRI < 1) && (chiTieuChinhThucDHQG[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuChinhThucDHQG[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Value = tongThucHien[i];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((tongThucHien[i] < 1) && (tongThucHien[i] > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (tongThucHien[i] % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        if (chiTieuChinhThucDHQG[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI > 0 ?
                                //Math.Round(((float)tongThucHien[i] / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, 2) : 0;
                                Math.Round(((float)tongThucHien[i] / chiTieuChinhThucDHQG[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                        }
                        else cellRange2.Value = string.Empty;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        //cellRange2.Style.Numberformat.Format = "###.##";
                    }
                }
            }
            #region In Tong chi tieu (neu co)
            if (hasDieuChinh)
            {
                if ((cungDonViTinh) && (currDonViTinh != "%"))
                {
                    cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                    cellRange2.Value = dvPhuTrachCTCap2;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                    cellRange2.Value = currDonViTinh;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                    cellRange2.Value = tongKH;

                    if ((tongKH < 1) && (tongKH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongKH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                    cellRange2.Value = tongDC;

                    if ((tongDC < 1) && (tongDC > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongDC % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                    cellRange2.Value = tongTH;

                    if ((tongTH < 1) && (tongTH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongTH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 9];
                    cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 10];
                    if (tongDC > 0) cellRange2.Value = Math.Round((tongTH / tongDC) * 100, MidpointRounding.AwayFromZero);
                    else cellRange2.Value = "";
                    cungDonViTinh = false;
                }
            }
            else
            {
                if ((cungDonViTinh) && (currDonViTinh != "%"))
                {
                    cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                    cellRange2.Value = dvPhuTrachCTCap2;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                    cellRange2.Value = currDonViTinh;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                    cellRange2.Value = tongKH;

                    if ((tongKH < 1) && (tongKH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongKH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                    cellRange2.Value = tongTH;

                    if ((tongTH < 1) && (tongTH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongTH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                    cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                    cungDonViTinh = false;
                }
            }
            #endregion

            return excelPackage;

        }
        #endregion

        #region Bao cao tien do thuc hien cua Don vi
        public FileResult ExportKHTHDonVi(int namKHId, string namKH, int nhomDonViId, int mocKS, int donViId)
        {
            ExcelPackage excelPackage;
            if (donViId == -1)
                excelPackage = getExcelExportDataDonViByNhom(namKHId, namKH, mocKS, nhomDonViId);
            //excelPackage = getExcelExportDataDonViAll(namKHId, namKH);
            else
                excelPackage = getExcelExportDataDonVi(namKHId, namKH, donViId, mocKS);

            FileResult result = File(excelPackage.GetAsByteArray(),
                "application/octet-stream", "TinhHinhThucHienKeHoachDHQG" + namKH + ".xlsx");
            excelPackage.Dispose();

            return result;

        }

        public FileResult PrintDataDonVi(int namKHId, string namKH, int nhomDonViId, int mocKS, int donViId)
        {
            ExcelPackage excelPackage;
            if (donViId == -1)
                excelPackage = getExcelExportDataDonViByNhom(namKHId, namKH, mocKS, nhomDonViId);
            //excelPackage = getExcelExportDataDonViAll(namKHId, namKH);
            else
                excelPackage = getExcelExportDataDonVi(namKHId, namKH, donViId, mocKS);

            MemoryStream inStream = new MemoryStream(excelPackage.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "KeHoachThucHienDHQG");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            excelPackage.Dispose();
            inStream.Dispose();

            return null;

        }

        public ExcelPackage getExcelExportDataDonVi(int namKHId, string namKH, int donViId, int mocKS)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<ExportExcell> chiTieuChinhThuc;

            int nhomDonViId = 0;
            string tenDV = "";
            //List<DonVi> listDonVi = new List<DonVi>();
            //listDonVi.Add(donvibo.GetById(donViId));
            DonVi dvbc = donvibo.GetById(donViId);
            if (dvbc != null) tenDV = dvbc.TenVietTat;

            ExportExcellBO exBO = new ExportExcellBO();
            bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

            if (coKeHoachDC)
            {
                chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, donViId, nhomDonViId);
            }
            else
            {
                chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, nhomDonViId);
            }

            List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom2(namKHId, laChiTieuChinhThuc, donViId, mocKS, nhomDonViId);
            List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach_TheoDonVi(namKHId, 0, donViId);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            //using (var excelPackage = new ExcelPackage())
            //{
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, 1000, 10];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
            //if (hasDieuChinh) titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];

            titleCell.Merge = true;
            titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin moc kiem soat
            int namkh = 0;
            if (!string.IsNullOrEmpty(namKH))
            {
                try
                {
                    namkh = System.Convert.ToInt32(namKH);
                }
                catch { }
            }
            MocKiemSoatBO mc = new MocKiemSoatBO();
            MocKiemSoat mks = mc.GetbyID(mocKS); //mc.GetMocKSIsActive(namkh);

            string kyBC = string.Empty;
            if (mks != null)
            {
                kyBC = mks.MOC_KIEM_SOAT;
            }
            startRowTile += 1;
            var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
            //if (hasDieuChinh) kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 9];
            kyBaoCaoCell.Merge = true;
            kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
            kyBaoCaoCell.Style.Font.Bold = true;
            kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 44;
            //if (hasDieuChinh) sheet.Column(3).Width = 28;

            sheet.Column(4).Width = 10;
            sheet.Column(5).Width = 10;
            sheet.Column(6).Width = 8;
            sheet.Column(7).Width = 8;
            sheet.Column(8).Width = 8;
            sheet.Column(9).Width = 8;
            sheet.Column(10).Width = 8;
            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "A";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;


            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "CÁC CHỈ TIÊU CHUNG";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            var cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 7];
            //if (hasDieuChinh) cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 9];

            cellRange2.Merge = true;
            cellRange2.Value = tenDV;// "ĐHQGHN";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            cellRange2 = sheet.Cells[startRow + 1, startCol + 5];
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Value = "KH";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Value = "TH";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Value = "%";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;


            int beginRow = startRow + 1;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            bool cungDonViTinh = false;
            string currDonViTinh = "";
            double tongKH = 0;
            double tongTH = 0;
            string dvPhuTrachCTCap2 = "";
            int rowIndexCTCap2 = 0;
            for (int i = 0; i < chiTieuChinhThuc.Count; i++)
            {
                //if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                //    chiTieuChinhThucDHQG[i].CAP == 2)
                if (chiTieuChinhThuc[i].CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    cellRange2 = sheet.Cells[beginRow, startCol, beginRow, startCol + 4];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU.ToUpper();

                    #region Temp
                    //if (hasDieuChinh)
                    //{
                    //    //KH
                    //    cellRange2 = sheet.Cells[beginRow, startCol + 5];
                    //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //    //DC
                    //    cellRange2 = sheet.Cells[beginRow, startCol + 6];
                    //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //    //Tổng TH
                    //    cellRange2 = sheet.Cells[beginRow, startCol + 7];
                    //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //    //%(KH)
                    //    cellRange2 = sheet.Cells[beginRow, startCol + 8];
                    //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //    //%(DC)
                    //    cellRange2 = sheet.Cells[beginRow, startCol + 9];
                    //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //}
                    //else 
                    #endregion
                    {
                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    #endregion
                }
                else if (chiTieuChinhThuc[i].CAP == 2)
                {
                    if (chiTieuChinhThuc[i].LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        #region In Tong chi tieu (neu co)
                        if ((cungDonViTinh) && (currDonViTinh != "%"))
                        {
                            cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                            cellRange2.Value = dvPhuTrachCTCap2;

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                            cellRange2.Value = currDonViTinh;

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                            cellRange2.Value = tongKH;

                            if ((tongKH < 1) && (tongKH > 0))
                            {
                                cellRange2.Style.Numberformat.Format = "0.##";
                            }
                            else if (tongKH % 1 == 0)
                            {
                                cellRange2.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                            cellRange2.Value = tongTH;

                            if ((tongTH < 1) && (tongTH > 0))
                            {
                                cellRange2.Style.Numberformat.Format = "0.##";
                            }
                            else if (tongTH % 1 == 0)
                            {
                                cellRange2.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                            cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                            cungDonViTinh = false;
                        }
                        #endregion

                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        cungDonViTinh = true;
                        currDonViTinh = "";
                        tongKH = 0;
                        tongTH = 0;

                        chiTieuAo = false;
                        #region Header
                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        #endregion

                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        //cellRange2.Style.Numberformat.Format = "###,###,###";
                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        //cellRange2.Style.Numberformat.Format = "###,###,###";

                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                }
                else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                {
                    beginRow++;
                    //chiTieuAo = false;
                    if (chiTieuAo)
                    {
                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        if (sttLevel2 == 1)
                        {
                            dvPhuTrachCTCap2 = donViPhuTrach[i].DON_VI;
                            currDonViTinh = chiTieuChinhThuc[i].DON_VI_TINH;
                            cungDonViTinh = true;
                            tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                            tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                        }
                        else
                        {
                            if (cungDonViTinh)
                            {
                                if ((currDonViTinh != null) && (chiTieuChinhThuc[i].DON_VI_TINH != null) &&
                                    (currDonViTinh.Trim().ToUpper() == chiTieuChinhThuc[i].DON_VI_TINH.Trim().ToUpper()))
                                {
                                    cungDonViTinh = true;
                                    tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                    tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                                }
                                else
                                {
                                    cungDonViTinh = false;
                                }
                            }

                            if (dvPhuTrachCTCap2 != donViPhuTrach[i].DON_VI) dvPhuTrachCTCap2 = "";
                        }

                        //STT
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    cellRange2 = sheet.Cells[beginRow, startCol + 2];
                    cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //Don vi giam sat
                    cellRange2 = sheet.Cells[beginRow, startCol + 3];
                    //cellRange2.Value = "Đơn vị giám sát";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.WrapText = true;

                    if (i < donViPhuTrach.Count) cellRange2.Value = donViPhuTrach[i].DON_VI ?? string.Empty;

                    //Don vi tinh
                    cellRange2 = sheet.Cells[beginRow, startCol + 4];
                    cellRange2.Value = chiTieuChinhThuc[i].DON_VI_TINH;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange2.Style.WrapText = true;


                    //KH
                    cellRange2 = sheet.Cells[beginRow, startCol + 5];
                    cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    if ((chiTieuChinhThuc[i].GIA_TRI < 1) && (chiTieuChinhThuc[i].GIA_TRI > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (chiTieuChinhThuc[i].GIA_TRI % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";


                    //Tổng TH
                    cellRange2 = sheet.Cells[beginRow, startCol + 6];
                    cellRange2.Value = keHoachThucHien[i].GIA_TRI;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    if ((keHoachThucHien[i].GIA_TRI < 1) && (keHoachThucHien[i].GIA_TRI > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (keHoachThucHien[i].GIA_TRI % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    //%(KH)
                    cellRange2 = sheet.Cells[beginRow, startCol + 7];
                    cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI > 0 ?
                        Math.Round(((float)keHoachThucHien[i].GIA_TRI / chiTieuChinhThuc[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    //cellRange2.Style.Numberformat.Format = "###.##";

                }
            }
            #region In Tong chi tieu (neu co)
            if ((cungDonViTinh) && (currDonViTinh != "%"))
            {
                cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                cellRange2.Value = dvPhuTrachCTCap2;

                cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                cellRange2.Value = currDonViTinh;

                cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                cellRange2.Value = tongKH;

                if ((tongKH < 1) && (tongKH > 0))
                {
                    cellRange2.Style.Numberformat.Format = "0.##";
                }
                else if (tongKH % 1 == 0)
                {
                    cellRange2.Style.Numberformat.Format = "###,###,###";
                }
                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                cellRange2.Value = tongTH;

                if ((tongTH < 1) && (tongTH > 0))
                {
                    cellRange2.Style.Numberformat.Format = "0.##";
                }
                else if (tongTH % 1 == 0)
                {
                    cellRange2.Style.Numberformat.Format = "###,###,###";
                }
                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                cungDonViTinh = false;
            }
            #endregion

            #region Chi tieu rieng
            List<ExportExcell> chiTieuRiengDauNam = sqlBO.GetListChiTieuRiengDonVi_KeHoach(namKHId, donViId, 21);
            List<ExportExcell> chiTieuRiengThucHien = sqlBO.GetListChiTieuRiengDonVi_KeHoachThucHien(namKHId, donViId);
            if (chiTieuRiengDauNam.Count > 0)
            {
                int startRowChiTieuRieng = beginRow + 1;

                ExcelRange cotStt = sheet.Cells[startRowChiTieuRieng, 1, startRowChiTieuRieng, 2];
                ExcelRange cotChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 2];
                ExcelRange cotDonviTinh = sheet.Cells[startRowChiTieuRieng, startCol + 3, startRowChiTieuRieng, startCol + 4];
                ExcelRange cotGiaTriChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 5, startRowChiTieuRieng, startCol + 5];
                ExcelRange cotThucHien = sheet.Cells[startRowChiTieuRieng, startCol + 5];
                ExcelRange cotTiLe = sheet.Cells[startRowChiTieuRieng, startCol + 7];

                cotStt.Merge = true;
                cotStt.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cotStt.Value = "B";
                cotStt.Style.Font.Bold = true;
                cotStt.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cotStt.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                cotChiTieu.Value = "CÁC CHỈ TIÊU ĐẶC THÙ CỦA ĐƠN VỊ";
                cotChiTieu.Style.Font.Bold = true;
                cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cotChiTieu.Style.WrapText = true;
                cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                cotDonviTinh.Merge = true;
                cotDonviTinh.Value = "Đơn vị tính";
                cotDonviTinh.Style.Font.Bold = true;
                cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cotDonviTinh.Style.WrapText = true;
                cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                cotGiaTriChiTieu.Merge = true;
                cotGiaTriChiTieu.Value = "Mức chỉ tiêu năm " + namKH + " đơn vị đăng ký";
                cotGiaTriChiTieu.Style.Font.Bold = true;
                cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cotGiaTriChiTieu.Style.WrapText = true;
                cotGiaTriChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                cotThucHien.Style.Font.Bold = true;
                cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cotThucHien.Style.WrapText = true;

                cotTiLe.Style.Font.Bold = true;
                cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cotTiLe.Style.WrapText = true;
                beginRow = startRowChiTieuRieng;
                int stt = 0;
                ExcelRange cellSTT = null;
                for (int i = 0; i < chiTieuRiengDauNam.Count; i++)
                {
                    if (chiTieuRiengDauNam[i].LA_CHI_TIEU_AO == 1)
                    {
                        continue;
                    }
                    beginRow++;

                    if (chiTieuRiengDauNam[i].CAP == 1)
                    {
                        cotChiTieu = sheet.Cells[beginRow, startCol, beginRow, 5];
                        cotChiTieu.Merge = true;
                        cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU.ToUpper();
                        cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;

                        cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                        cotThucHien = sheet.Cells[beginRow, startCol + 6];
                        cotTiLe = sheet.Cells[beginRow, startCol + 7];

                        cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                        cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                        cotThucHien = sheet.Cells[beginRow, startCol + 6];
                        cotTiLe = sheet.Cells[beginRow, startCol + 7];

                        cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    else if (chiTieuRiengDauNam[i].CAP == 2)
                    {
                        stt++;
                        cellSTT = sheet.Cells[beginRow, 1];
                        cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellSTT.Value = (stt).ToString();

                        cellSTT = sheet.Cells[beginRow, 2];
                        cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                        cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                        cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                        cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                        cotThucHien = sheet.Cells[beginRow, startCol + 6];
                        cotTiLe = sheet.Cells[beginRow, startCol + 7];


                        cotChiTieu.Style.WrapText = true;
                        cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                        cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                        cotChiTieu.Style.Font.Bold = true;

                        cotDonviTinh.Merge = true;

                        cotDonviTinh.Style.WrapText = true;
                        cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                        cotGiaTriChiTieu.Merge = true;

                        cotGiaTriChiTieu.Style.WrapText = true;
                        cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cotThucHien.Style.WrapText = true;
                        cotThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                        cotTiLe.Style.WrapText = true;
                        cotTiLe.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    else
                    {
                        //cotDonviTinh.Value = (chiTieuRiengDauNam[i].DON_VI_TINH.ToUpper() == "NA" ?
                        //    string.Empty : chiTieuRiengDauNam[i].DON_VI_TINH);
                        stt++;

                        cellSTT = sheet.Cells[beginRow, 1];
                        cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellSTT.Value = (stt).ToString();


                        cellSTT = sheet.Cells[beginRow, 2];
                        cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);


                        cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                        cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                        cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                        cotThucHien = sheet.Cells[beginRow, startCol + 6];
                        cotTiLe = sheet.Cells[beginRow, startCol + 7];

                        cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                        cotDonviTinh.Value = chiTieuRiengDauNam[i].DON_VI_TINH;
                        cotDonviTinh.Merge = true;
                        cotGiaTriChiTieu.Value = chiTieuRiengDauNam[i].GIA_TRI;
                        //cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";
                        if ((chiTieuRiengDauNam[i].GIA_TRI < 1) && (chiTieuRiengDauNam[i].GIA_TRI > 0))
                        {
                            cotGiaTriChiTieu.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuRiengDauNam[i].GIA_TRI % 1 == 0)
                        {
                            cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";
                        }
                        else cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###.##";

                        cotThucHien.Value = chiTieuRiengThucHien[i].GIA_TRI;
                        //cotThucHien.Style.Numberformat.Format = "###,###,###";

                        if ((chiTieuRiengThucHien[i].GIA_TRI < 1) && (chiTieuRiengThucHien[i].GIA_TRI > 0))
                        {
                            cotThucHien.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuRiengThucHien[i].GIA_TRI % 1 == 0)
                        {
                            cotThucHien.Style.Numberformat.Format = "###,###,###";
                        }
                        else cotThucHien.Style.Numberformat.Format = "###,###,###.##";

                        cotTiLe.Value = (chiTieuRiengDauNam[i].GIA_TRI > 0 ?
                            Math.Round((chiTieuRiengThucHien[i].GIA_TRI / chiTieuRiengDauNam[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0);
                    }

                    cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
            }
            #endregion

            return excelPackage;
        }

        public ExcelPackage getExcelExportDataDonViByNhom(int namKHId, string namKH, int mocKS, int nhomDonViId)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<ExportExcell> chiTieuChinhThuc;

            int donViId;
            string tenDV = "";
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();

            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";

            DonViBO donviBO = new DonViBO();
            List<DonVi> listDonVi = donviBO.GetAllDonViByNhom(nhomDonViId);

            foreach (DonVi iDonVi in listDonVi)
            {
                donViId = iDonVi.DonViId;
                donviBO = new DonViBO();
                DonVi dvbc = donviBO.GetById(donViId);
                if (dvbc != null) tenDV = dvbc.TenVietTat;

                ExportExcellBO exBO = new ExportExcellBO();
                bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

                if (coKeHoachDC)
                {
                    chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, donViId, 0);
                }
                else
                {
                    chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, 0);
                }

                //List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, 0);
                List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom2(namKHId, laChiTieuChinhThuc, donViId, mocKS, 0);
                List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach_TheoDonVi(namKHId, 0, donViId);

                List<ExportExcell> chiTieuRiengDauNam = sqlBO.GetListChiTieuRiengDonVi_KeHoach(namKHId, donViId, 21);
                List<ExportExcell> chiTieuRiengThucHien = sqlBO.GetListChiTieuRiengDonVi_KeHoachThucHien(namKHId, donViId);

                var sheet = excelPackage.Workbook.Worksheets.Add(tenDV);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, chiTieuChinhThuc.Count + chiTieuRiengDauNam.Count + 5, 10];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = tenDV;
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                //if (hasDieuChinh) titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];

                titleCell.Merge = true;
                titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Thong tin moc kiem soat
                int namkh = 0;
                if (!string.IsNullOrEmpty(namKH))
                {
                    try
                    {
                        namkh = System.Convert.ToInt32(namKH);
                    }
                    catch { }
                }
                MocKiemSoatBO mc = new MocKiemSoatBO();
                MocKiemSoat mks = mc.GetbyID(mocKS); //mc.GetMocKSIsActive(namkh);

                string kyBC = string.Empty;
                if (mks != null)
                {
                    kyBC = mks.MOC_KIEM_SOAT;
                }
                startRowTile += 1;
                var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                //if (hasDieuChinh) kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 9];
                kyBaoCaoCell.Merge = true;
                kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
                kyBaoCaoCell.Style.Font.Bold = true;
                kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 4;
                sheet.Column(2).Width = 5;
                sheet.Column(3).Width = 44;
                //if (hasDieuChinh) sheet.Column(3).Width = 28;

                sheet.Column(4).Width = 10;
                sheet.Column(5).Width = 10;
                sheet.Column(6).Width = 8;
                sheet.Column(7).Width = 8;
                sheet.Column(8).Width = 8;
                sheet.Column(9).Width = 8;
                sheet.Column(10).Width = 8;
                #endregion

                startRow = startRowTile + 1;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol + 1];
                cellRange0.Merge = true;
                cellRange0.Value = "A";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;


                //Chitieu
                var cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "CÁC CHỈ TIÊU CHUNG";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;

                //Don vi giam sat
                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
                cellRange1.Merge = true;
                cellRange1.Value = "ĐV phân bổ và giám sát";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                var cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 7];
                //if (hasDieuChinh) cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 9];

                cellRange2.Merge = true;
                cellRange2.Value = tenDV;// "ĐHQGHN";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 5];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "KH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "TH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;


                int beginRow = startRow + 1;
                int beginCol = startCol;
                int sttLevel1 = 1;
                int sttLevel2 = 1;
                bool chiTieuAo = false;
                bool cungDonViTinh = false;
                string currDonViTinh = "";
                double tongKH = 0;
                double tongTH = 0;
                string dvPhuTrachCTCap2 = "";
                int rowIndexCTCap2 = 0;
                for (int i = 0; i < chiTieuChinhThuc.Count; i++)
                {
                    //if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                    //    chiTieuChinhThucDHQG[i].CAP == 2)
                    if (chiTieuChinhThuc[i].CAP == 1)
                    {
                        #region Chi tieu cap 1
                        beginRow++;
                        cellRange2 = sheet.Cells[beginRow, startCol, beginRow, startCol + 4];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU.ToUpper();

                        #region Temp
                        //if (hasDieuChinh)
                        //{
                        //    //KH
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //    //DC
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //    //Tổng TH
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //    //%(KH)
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 8];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //    //%(DC)
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 9];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //}
                        //else 
                        #endregion
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        #endregion
                    }
                    else if (chiTieuChinhThuc[i].CAP == 2)
                    {
                        if (chiTieuChinhThuc[i].LA_CHI_TIEU_AO == 1)
                        {
                            chiTieuAo = true;
                        }
                        else
                        {
                            #region In Tong chi tieu (neu co)
                            if ((cungDonViTinh) && (currDonViTinh != "%"))
                            {
                                cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                                cellRange2.Value = dvPhuTrachCTCap2;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                                cellRange2.Value = currDonViTinh;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                                cellRange2.Value = tongKH;

                                if ((tongKH < 1) && (tongKH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongKH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                                cellRange2.Value = tongTH;

                                if ((tongTH < 1) && (tongTH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongTH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                                cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                                cungDonViTinh = false;
                            }
                            #endregion

                            beginRow++;

                            rowIndexCTCap2 = beginRow;
                            cungDonViTinh = true;
                            currDonViTinh = "";
                            tongKH = 0;
                            tongTH = 0;

                            chiTieuAo = false;
                            #region Header
                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = "";

                            //Ten chi tieu
                            cellRange2 = sheet.Cells[beginRow, startCol + 2];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;

                            //Don vi giam sat
                            cellRange2 = sheet.Cells[beginRow, startCol + 3];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.WrapText = true;

                            //Don vi tinh
                            cellRange2 = sheet.Cells[beginRow, startCol + 4];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            #endregion

                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";

                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;
                        }
                    }
                    else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                    {
                        beginRow++;
                        //chiTieuAo = false;
                        if (chiTieuAo)
                        {
                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;

                        }
                        else
                        {
                            if (sttLevel2 == 1)
                            {
                                dvPhuTrachCTCap2 = donViPhuTrach[i].DON_VI;
                                currDonViTinh = chiTieuChinhThuc[i].DON_VI_TINH;
                                cungDonViTinh = true;
                                tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                            }
                            else
                            {
                                if (cungDonViTinh)
                                {
                                    if ((currDonViTinh != null) && (chiTieuChinhThuc[i].DON_VI_TINH != null) &&
                                        (currDonViTinh.Trim().ToUpper() == chiTieuChinhThuc[i].DON_VI_TINH.Trim().ToUpper()))
                                    {
                                        cungDonViTinh = true;
                                        tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                        tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                                    }
                                    else
                                    {
                                        cungDonViTinh = false;
                                    }
                                }

                                if (dvPhuTrachCTCap2 != donViPhuTrach[i].DON_VI) dvPhuTrachCTCap2 = "";
                            }

                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            sttLevel2 = sttLevel2 + 1;
                        }

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        //cellRange2.Value = "Đơn vị giám sát";
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        if (i < donViPhuTrach.Count) cellRange2.Value = donViPhuTrach[i].DON_VI ?? string.Empty;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Value = chiTieuChinhThuc[i].DON_VI_TINH;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.WrapText = true;


                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((chiTieuChinhThuc[i].GIA_TRI < 1) && (chiTieuChinhThuc[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuChinhThuc[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";


                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Value = keHoachThucHien[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((keHoachThucHien[i].GIA_TRI < 1) && (keHoachThucHien[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (keHoachThucHien[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI > 0 ?
                            Math.Round(((float)keHoachThucHien[i].GIA_TRI / chiTieuChinhThuc[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        //cellRange2.Style.Numberformat.Format = "###.##";

                    }
                }
                #region In Tong chi tieu (neu co)
                if ((cungDonViTinh) && (currDonViTinh != "%"))
                {
                    cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                    cellRange2.Value = dvPhuTrachCTCap2;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                    cellRange2.Value = currDonViTinh;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                    cellRange2.Value = tongKH;

                    if ((tongKH < 1) && (tongKH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongKH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                    cellRange2.Value = tongTH;

                    if ((tongTH < 1) && (tongTH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongTH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                    cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                    cungDonViTinh = false;
                }
                #endregion

                #region Chi tieu rieng

                if (chiTieuRiengDauNam.Count > 0)
                {
                    int startRowChiTieuRieng = beginRow + 1;

                    ExcelRange cotStt = sheet.Cells[startRowChiTieuRieng, 1, startRowChiTieuRieng, 2];
                    ExcelRange cotChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 2];
                    ExcelRange cotDonviTinh = sheet.Cells[startRowChiTieuRieng, startCol + 3, startRowChiTieuRieng, startCol + 4];
                    ExcelRange cotGiaTriChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 5, startRowChiTieuRieng, startCol + 5];
                    ExcelRange cotThucHien = sheet.Cells[startRowChiTieuRieng, startCol + 5];
                    ExcelRange cotTiLe = sheet.Cells[startRowChiTieuRieng, startCol + 7];

                    cotStt.Merge = true;
                    cotStt.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotStt.Value = "B";
                    cotStt.Style.Font.Bold = true;
                    cotStt.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotStt.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotChiTieu.Value = "CÁC CHỈ TIÊU ĐẶC THÙ CỦA ĐƠN VỊ";
                    cotChiTieu.Style.Font.Bold = true;
                    cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotChiTieu.Style.WrapText = true;
                    cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotDonviTinh.Merge = true;
                    cotDonviTinh.Value = "Đơn vị tính";
                    cotDonviTinh.Style.Font.Bold = true;
                    cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotDonviTinh.Style.WrapText = true;
                    cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotGiaTriChiTieu.Merge = true;
                    cotGiaTriChiTieu.Value = "Mức chỉ tiêu năm " + namKH + " đơn vị đăng ký";
                    cotGiaTriChiTieu.Style.Font.Bold = true;
                    cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotGiaTriChiTieu.Style.WrapText = true;
                    cotGiaTriChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotThucHien.Style.Font.Bold = true;
                    cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotThucHien.Style.WrapText = true;

                    cotTiLe.Style.Font.Bold = true;
                    cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotTiLe.Style.WrapText = true;
                    beginRow = startRowChiTieuRieng;
                    int stt = 0;
                    ExcelRange cellSTT = null;
                    for (int i = 0; i < chiTieuRiengDauNam.Count; i++)
                    {
                        if (chiTieuRiengDauNam[i].LA_CHI_TIEU_AO == 1)
                        {
                            continue;
                        }
                        beginRow++;

                        if (chiTieuRiengDauNam[i].CAP == 1)
                        {
                            cotChiTieu = sheet.Cells[beginRow, startCol, beginRow, 5];
                            cotChiTieu.Merge = true;
                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU.ToUpper();
                            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;

                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else if (chiTieuRiengDauNam[i].CAP == 2)
                        {
                            stt++;
                            cellSTT = sheet.Cells[beginRow, 1];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellSTT.Value = (stt).ToString();

                            cellSTT = sheet.Cells[beginRow, 2];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];


                            cotChiTieu.Style.WrapText = true;
                            cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                            cotChiTieu.Style.Font.Bold = true;

                            cotDonviTinh.Merge = true;

                            cotDonviTinh.Style.WrapText = true;
                            cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                            cotGiaTriChiTieu.Merge = true;

                            cotGiaTriChiTieu.Style.WrapText = true;
                            cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cotThucHien.Style.WrapText = true;
                            cotThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                            cotTiLe.Style.WrapText = true;
                            cotTiLe.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else
                        {
                            //cotDonviTinh.Value = (chiTieuRiengDauNam[i].DON_VI_TINH.ToUpper() == "NA" ?
                            //    string.Empty : chiTieuRiengDauNam[i].DON_VI_TINH);
                            stt++;

                            cellSTT = sheet.Cells[beginRow, 1];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellSTT.Value = (stt).ToString();


                            cellSTT = sheet.Cells[beginRow, 2];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);


                            cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                            cotDonviTinh.Value = chiTieuRiengDauNam[i].DON_VI_TINH;
                            cotDonviTinh.Merge = true;
                            cotGiaTriChiTieu.Value = chiTieuRiengDauNam[i].GIA_TRI;
                            //cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";

                            if ((chiTieuRiengDauNam[i].GIA_TRI < 1) && (chiTieuRiengDauNam[i].GIA_TRI > 0))
                            {
                                cotGiaTriChiTieu.Style.Numberformat.Format = "0.##";
                            }
                            else if (chiTieuRiengDauNam[i].GIA_TRI % 1 == 0)
                            {
                                cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";
                            }
                            else cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###.##";

                            cotThucHien.Value = chiTieuRiengThucHien[i].GIA_TRI;
                            //cotThucHien.Style.Numberformat.Format = "###,###,###";

                            if ((chiTieuRiengThucHien[i].GIA_TRI < 1) && (chiTieuRiengThucHien[i].GIA_TRI > 0))
                            {
                                cotThucHien.Style.Numberformat.Format = "0.##";
                            }
                            else if (chiTieuRiengThucHien[i].GIA_TRI % 1 == 0)
                            {
                                cotThucHien.Style.Numberformat.Format = "###,###,###";
                            }
                            else cotThucHien.Style.Numberformat.Format = "###,###,###.##";

                            cotTiLe.Value = (chiTieuRiengDauNam[i].GIA_TRI > 0 ?
                                Math.Round((chiTieuRiengThucHien[i].GIA_TRI / chiTieuRiengDauNam[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0);
                        }

                        cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    }
                }
                #endregion
            }
            return excelPackage;
        }

        public ExcelPackage getExcelExportDataDonViAll(int namKHId, string namKH, int mocKS)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<ExportExcell> chiTieuChinhThuc;

            int donViId;
            int nhomDonViId = 0;
            string tenDV = "";
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();

            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";

            DonViBO donviBO = new DonViBO();
            List<DonVi> listDonVi = donviBO.GetDonViByType(false);

            foreach (DonVi iDonVi in listDonVi)
            {
                donViId = iDonVi.DonViId;
                donviBO = new DonViBO();
                DonVi dvbc = donviBO.GetById(donViId);
                if (dvbc != null) tenDV = dvbc.TenVietTat;

                ExportExcellBO exBO = new ExportExcellBO();
                bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

                if (coKeHoachDC)
                {
                    chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, donViId, nhomDonViId);
                }
                else
                {
                    chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, nhomDonViId);
                }

                //List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, nhomDonViId);
                List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom2(namKHId, laChiTieuChinhThuc, donViId, mocKS, nhomDonViId);
                List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach_TheoDonVi(namKHId, 0, donViId);

                List<ExportExcell> chiTieuRiengDauNam = sqlBO.GetListChiTieuRiengDonVi_KeHoach(namKHId, donViId, 21);
                List<ExportExcell> chiTieuRiengThucHien = sqlBO.GetListChiTieuRiengDonVi_KeHoachThucHien(namKHId, donViId);

                var sheet = excelPackage.Workbook.Worksheets.Add(tenDV);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, chiTieuChinhThuc.Count + chiTieuRiengDauNam.Count + 5, 10];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = tenDV;
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                //if (hasDieuChinh) titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];

                titleCell.Merge = true;
                titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Thong tin moc kiem soat
                int namkh = 0;
                if (!string.IsNullOrEmpty(namKH))
                {
                    try
                    {
                        namkh = System.Convert.ToInt32(namKH);
                    }
                    catch { }
                }
                MocKiemSoatBO mc = new MocKiemSoatBO();
                MocKiemSoat mks = mc.GetbyID(mocKS); //mc.GetMocKSIsActive(namkh);

                string kyBC = string.Empty;
                if (mks != null)
                {
                    kyBC = mks.MOC_KIEM_SOAT;
                }
                startRowTile += 1;
                var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                //if (hasDieuChinh) kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 9];
                kyBaoCaoCell.Merge = true;
                kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
                kyBaoCaoCell.Style.Font.Bold = true;
                kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 4;
                sheet.Column(2).Width = 5;
                sheet.Column(3).Width = 44;
                //if (hasDieuChinh) sheet.Column(3).Width = 28;

                sheet.Column(4).Width = 10;
                sheet.Column(5).Width = 10;
                sheet.Column(6).Width = 8;
                sheet.Column(7).Width = 8;
                sheet.Column(8).Width = 8;
                sheet.Column(9).Width = 8;
                sheet.Column(10).Width = 8;
                #endregion

                startRow = startRowTile + 1;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol + 1];
                cellRange0.Merge = true;
                cellRange0.Value = "A";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;


                //Chitieu
                var cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "CÁC CHỈ TIÊU CHUNG";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;

                //Don vi giam sat
                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
                cellRange1.Merge = true;
                cellRange1.Value = "ĐV phân bổ và giám sát";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                var cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 7];
                //if (hasDieuChinh) cellRange2 = sheet.Cells[startRow, startCol + 5, startRow, startCol + 9];

                cellRange2.Merge = true;
                cellRange2.Value = tenDV;// "ĐHQGHN";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 5];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "KH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 6];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "TH";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow + 1, startCol + 7];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Value = "%";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;


                int beginRow = startRow + 1;
                int beginCol = startCol;
                int sttLevel1 = 1;
                int sttLevel2 = 1;
                bool chiTieuAo = false;
                bool cungDonViTinh = false;
                string currDonViTinh = "";
                double tongKH = 0;
                double tongTH = 0;
                string dvPhuTrachCTCap2 = "";
                int rowIndexCTCap2 = 0;
                for (int i = 0; i < chiTieuChinhThuc.Count; i++)
                {
                    //if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                    //    chiTieuChinhThucDHQG[i].CAP == 2)
                    if (chiTieuChinhThuc[i].CAP == 1)
                    {
                        #region Chi tieu cap 1
                        beginRow++;
                        cellRange2 = sheet.Cells[beginRow, startCol, beginRow, startCol + 4];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU.ToUpper();

                        #region Temp
                        //if (hasDieuChinh)
                        //{
                        //    //KH
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //    //DC
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //    //Tổng TH
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //    //%(KH)
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 8];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //    //%(DC)
                        //    cellRange2 = sheet.Cells[beginRow, startCol + 9];
                        //    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //}
                        //else 
                        #endregion
                        {
                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        #endregion
                    }
                    else if (chiTieuChinhThuc[i].CAP == 2)
                    {
                        if (chiTieuChinhThuc[i].LA_CHI_TIEU_AO == 1)
                        {
                            chiTieuAo = true;
                        }
                        else
                        {
                            #region In Tong chi tieu (neu co)
                            if ((cungDonViTinh) && (currDonViTinh != "%"))
                            {
                                cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                                cellRange2.Value = dvPhuTrachCTCap2;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                                cellRange2.Value = currDonViTinh;

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                                cellRange2.Value = tongKH;

                                if ((tongKH < 1) && (tongKH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongKH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                                cellRange2.Value = tongTH;

                                if ((tongTH < 1) && (tongTH > 0))
                                {
                                    cellRange2.Style.Numberformat.Format = "0.##";
                                }
                                else if (tongTH % 1 == 0)
                                {
                                    cellRange2.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                                cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                                cungDonViTinh = false;
                            }
                            #endregion

                            beginRow++;

                            rowIndexCTCap2 = beginRow;
                            cungDonViTinh = true;
                            currDonViTinh = "";
                            tongKH = 0;
                            tongTH = 0;

                            chiTieuAo = false;
                            #region Header
                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = "";

                            //Ten chi tieu
                            cellRange2 = sheet.Cells[beginRow, startCol + 2];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;

                            //Don vi giam sat
                            cellRange2 = sheet.Cells[beginRow, startCol + 3];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.WrapText = true;

                            //Don vi tinh
                            cellRange2 = sheet.Cells[beginRow, startCol + 4];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            #endregion

                            //KH
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //Tổng TH
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";

                            //%(KH)
                            cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;
                        }
                    }
                    else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                    {
                        beginRow++;
                        //chiTieuAo = false;
                        if (chiTieuAo)
                        {
                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;

                        }
                        else
                        {
                            if (sttLevel2 == 1)
                            {
                                dvPhuTrachCTCap2 = donViPhuTrach[i].DON_VI;
                                currDonViTinh = chiTieuChinhThuc[i].DON_VI_TINH;
                                cungDonViTinh = true;
                                tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                            }
                            else
                            {
                                if (cungDonViTinh)
                                {
                                    if ((currDonViTinh != null) && (chiTieuChinhThuc[i].DON_VI_TINH != null) &&
                                        (currDonViTinh.Trim().ToUpper() == chiTieuChinhThuc[i].DON_VI_TINH.Trim().ToUpper()))
                                    {
                                        cungDonViTinh = true;
                                        tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                        tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                                    }
                                    else
                                    {
                                        cungDonViTinh = false;
                                    }
                                }

                                if (dvPhuTrachCTCap2 != donViPhuTrach[i].DON_VI) dvPhuTrachCTCap2 = "";
                            }

                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            sttLevel2 = sttLevel2 + 1;
                        }

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        //cellRange2.Value = "Đơn vị giám sát";
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        if (i < donViPhuTrach.Count) cellRange2.Value = donViPhuTrach[i].DON_VI ?? string.Empty;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Value = chiTieuChinhThuc[i].DON_VI_TINH;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.WrapText = true;


                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((chiTieuChinhThuc[i].GIA_TRI < 1) && (chiTieuChinhThuc[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuChinhThuc[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";


                        //Tổng TH
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Value = keHoachThucHien[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((keHoachThucHien[i].GIA_TRI < 1) && (keHoachThucHien[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (keHoachThucHien[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        //%(KH)
                        cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI > 0 ?
                            Math.Round(((float)keHoachThucHien[i].GIA_TRI / chiTieuChinhThuc[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        //cellRange2.Style.Numberformat.Format = "###.##";

                    }
                }
                #region In Tong chi tieu (neu co)
                if ((cungDonViTinh) && (currDonViTinh != "%"))
                {
                    cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                    cellRange2.Value = dvPhuTrachCTCap2;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                    cellRange2.Value = currDonViTinh;

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                    cellRange2.Value = tongKH;

                    if ((tongKH < 1) && (tongKH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongKH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                    cellRange2.Value = tongTH;

                    if ((tongTH < 1) && (tongTH > 0))
                    {
                        cellRange2.Style.Numberformat.Format = "0.##";
                    }
                    else if (tongTH % 1 == 0)
                    {
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                    cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                    cungDonViTinh = false;
                }
                #endregion

                #region Chi tieu rieng

                if (chiTieuRiengDauNam.Count > 0)
                {
                    int startRowChiTieuRieng = beginRow + 1;

                    ExcelRange cotStt = sheet.Cells[startRowChiTieuRieng, 1, startRowChiTieuRieng, 2];
                    ExcelRange cotChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 2];
                    ExcelRange cotDonviTinh = sheet.Cells[startRowChiTieuRieng, startCol + 3, startRowChiTieuRieng, startCol + 4];
                    ExcelRange cotGiaTriChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 5, startRowChiTieuRieng, startCol + 5];
                    ExcelRange cotThucHien = sheet.Cells[startRowChiTieuRieng, startCol + 5];
                    ExcelRange cotTiLe = sheet.Cells[startRowChiTieuRieng, startCol + 7];

                    cotStt.Merge = true;
                    cotStt.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotStt.Value = "B";
                    cotStt.Style.Font.Bold = true;
                    cotStt.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotStt.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotChiTieu.Value = "CÁC CHỈ TIÊU ĐẶC THÙ CỦA ĐƠN VỊ";
                    cotChiTieu.Style.Font.Bold = true;
                    cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotChiTieu.Style.WrapText = true;
                    cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotDonviTinh.Merge = true;
                    cotDonviTinh.Value = "Đơn vị tính";
                    cotDonviTinh.Style.Font.Bold = true;
                    cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotDonviTinh.Style.WrapText = true;
                    cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotGiaTriChiTieu.Merge = true;
                    cotGiaTriChiTieu.Value = "Mức chỉ tiêu năm " + namKH + " đơn vị đăng ký";
                    cotGiaTriChiTieu.Style.Font.Bold = true;
                    cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotGiaTriChiTieu.Style.WrapText = true;
                    cotGiaTriChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotThucHien.Style.Font.Bold = true;
                    cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotThucHien.Style.WrapText = true;

                    cotTiLe.Style.Font.Bold = true;
                    cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotTiLe.Style.WrapText = true;
                    beginRow = startRowChiTieuRieng;
                    int stt = 0;
                    ExcelRange cellSTT = null;
                    for (int i = 0; i < chiTieuRiengDauNam.Count; i++)
                    {
                        if (chiTieuRiengDauNam[i].LA_CHI_TIEU_AO == 1)
                        {
                            continue;
                        }
                        beginRow++;

                        if (chiTieuRiengDauNam[i].CAP == 1)
                        {
                            cotChiTieu = sheet.Cells[beginRow, startCol, beginRow, 5];
                            cotChiTieu.Merge = true;
                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU.ToUpper();
                            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;

                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else if (chiTieuRiengDauNam[i].CAP == 2)
                        {
                            stt++;
                            cellSTT = sheet.Cells[beginRow, 1];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellSTT.Value = (stt).ToString();

                            cellSTT = sheet.Cells[beginRow, 2];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];


                            cotChiTieu.Style.WrapText = true;
                            cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                            cotChiTieu.Style.Font.Bold = true;

                            cotDonviTinh.Merge = true;

                            cotDonviTinh.Style.WrapText = true;
                            cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                            cotGiaTriChiTieu.Merge = true;

                            cotGiaTriChiTieu.Style.WrapText = true;
                            cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cotThucHien.Style.WrapText = true;
                            cotThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                            cotTiLe.Style.WrapText = true;
                            cotTiLe.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else
                        {
                            //cotDonviTinh.Value = (chiTieuRiengDauNam[i].DON_VI_TINH.ToUpper() == "NA" ?
                            //    string.Empty : chiTieuRiengDauNam[i].DON_VI_TINH);
                            stt++;

                            cellSTT = sheet.Cells[beginRow, 1];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellSTT.Value = (stt).ToString();


                            cellSTT = sheet.Cells[beginRow, 2];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);


                            cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                            cotDonviTinh.Value = chiTieuRiengDauNam[i].DON_VI_TINH;
                            cotDonviTinh.Merge = true;
                            cotGiaTriChiTieu.Value = chiTieuRiengDauNam[i].GIA_TRI;
                            //cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";

                            if ((chiTieuRiengDauNam[i].GIA_TRI < 1) && (chiTieuRiengDauNam[i].GIA_TRI > 0))
                            {
                                cotGiaTriChiTieu.Style.Numberformat.Format = "0.##";
                            }
                            else if (chiTieuRiengDauNam[i].GIA_TRI % 1 == 0)
                            {
                                cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";
                            }
                            else cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###.##";

                            cotThucHien.Value = chiTieuRiengThucHien[i].GIA_TRI;
                            //cotThucHien.Style.Numberformat.Format = "###,###,###";

                            if ((chiTieuRiengThucHien[i].GIA_TRI < 1) && (chiTieuRiengThucHien[i].GIA_TRI > 0))
                            {
                                cotThucHien.Style.Numberformat.Format = "0.##";
                            }
                            else if (chiTieuRiengThucHien[i].GIA_TRI % 1 == 0)
                            {
                                cotThucHien.Style.Numberformat.Format = "###,###,###";
                            }
                            else cotThucHien.Style.Numberformat.Format = "###,###,###.##";

                            cotTiLe.Value = (chiTieuRiengDauNam[i].GIA_TRI > 0 ?
                                Math.Round((chiTieuRiengThucHien[i].GIA_TRI / chiTieuRiengDauNam[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0);
                        }

                        cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    }
                }
                #endregion
            }
            return excelPackage;
        }

        #endregion

        public FileResult ExportDataDonVi(int namKHId, int donViId, string namKH)
        {
            int nhomDonViId = 0;
            ExportExcellBO sqlBO = new ExportExcellBO();
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<DonVi> listDonVi = new List<DonVi>();
            listDonVi.Add(donvibo.GetById(donViId));

            List<List<ExportExcell>> dsChiTieuDonVi = new List<List<ExportExcell>>();
            List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
            List<ExportExcell> dataDonVi;
            for (int i = 0; i < listDonVi.Count; i++)
            {
                ExportExcellBO exBO = new ExportExcellBO();
                bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);
                if (coKeHoachDC)
                {
                    dataDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, listDonVi[i].DonViId, nhomDonViId);
                }
                else
                {
                    dataDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, listDonVi[i].DonViId, nhomDonViId);
                }
                dsChiTieuDonVi.Add(dataDonVi);
                dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, listDonVi[i].DonViId, nhomDonViId));
            }

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            using (var excelPackage = new ExcelPackage())
            {
                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, 1000, listDonVi.Count * 3 + 7];
                printArea.Style.Font.SetFromFont(new Font("Arial", 10));

                // output a line for the headers
                sheet.Name = "Tình hình thực hiện KH " + namKHId;
                int startRowLogo = 1;
                int startColumnLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                startRowLogo += 1;
                donViCell = sheet.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo];
                donViCell.Merge = true;
                donViCell.Value = listDonVi.Count > 0 ? listDonVi[0].TenDonVi.ToUpper() : string.Empty;
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = startRowLogo + 1;
                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, listDonVi.Count * 3 + 2];
                titleCell.Merge = true;
                titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Arial", 12));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Thong tin moc kiem soat
                int namkh = 0;
                if (!string.IsNullOrEmpty(namKH))
                {
                    try
                    {
                        namkh = System.Convert.ToInt32(namKH);
                    }
                    catch { }
                }
                MocKiemSoatBO mc = new MocKiemSoatBO();
                MocKiemSoat mks = mc.GetMocKSIsActive(namkh);

                string kyBC = string.Empty;
                if (mks != null)
                {
                    kyBC = mks.MOC_KIEM_SOAT;
                }
                startRowTile += 1;
                var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, listDonVi.Count * 3 + 2];
                kyBaoCaoCell.Merge = true;
                kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
                kyBaoCaoCell.Style.Font.Bold = true;
                kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                startRow = startRowTile + 1;
                var cellRange1 = sheet.Cells[startRow, startCol, startRow + 1, startCol];
                cellRange1.Merge = true;
                cellRange1.Value = "Chỉ tiêu";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                ExcelColumn colChiTieu = sheet.Column(startCol);
                colChiTieu.Style.WrapText = true;
                colChiTieu.Width = 60;

                cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                ExcelColumn colDonVi = sheet.Column(startCol + 1);
                colDonVi.Style.WrapText = true;
                colDonVi.Width = 15;
                ExcelRange cellRange2 = null;
                int colStart = startCol + 2;
                for (int i = 0; i < listDonVi.Count; i++)
                {
                    cellRange2 = sheet.Cells[startRow, colStart, startRow, colStart + 2];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = listDonVi[i].TenVietTat;

                    cellRange2 = sheet.Cells[startRow + 1, colStart];
                    cellRange2.Value = "KH";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    cellRange2 = sheet.Cells[startRow + 1, colStart + 1];
                    cellRange2.Value = "TH";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    cellRange2 = sheet.Cells[startRow + 1, colStart + 2];
                    cellRange2.Value = "%";
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    colStart = colStart + 3;
                }
                cellRange2 = sheet.Cells[startRow, startCol + 2, startRow + 1, colStart - 1];
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                int beginRow = startRow + 1;
                int beginCol = startCol;

                if (dsChiTieuDonVi == null || dsChiTieuDonVi.Count == 0)
                    return null;
                #region Temp
                List<ExportExcell> danhSachChiTieu = dsChiTieuDonVi[0];
                for (int i = 0; i < danhSachChiTieu.Count; i++)
                {
                    beginRow++;
                    cellRange2 = sheet.Cells[beginRow, startCol];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    if (danhSachChiTieu[i].CAP == 1)
                    {
                        cellRange2.Value = danhSachChiTieu[i].TEN_CHI_TIEU.ToUpper();
                    }
                    else cellRange2.Value = danhSachChiTieu[i].TEN_CHI_TIEU;

                    if (danhSachChiTieu[i].CAP == 1 ||
                        danhSachChiTieu[i].CAP == 2)
                    {
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    else
                    {
                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Value = danhSachChiTieu[i].DON_VI_TINH;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    }
                }
                cellRange2 = sheet.Cells[startRow + 1, startCol, beginRow, startCol + 1];
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                #endregion

                colStart = startCol + 2;
                beginRow = startRow + 1;
                for (int i = 0; i < dsChiTieuDonVi.Count; i++)
                {
                    int len = dsChiTieuDonVi[i].Count;
                    beginRow = startRow + 1;
                    for (int j = 0; j < len; j++)
                    {
                        beginRow++;
                        cellRange2 = sheet.Cells[beginRow, colStart];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        if (dsChiTieuDonVi[i][j].CAP > 2)
                        {
                            cellRange2 = sheet.Cells[beginRow, colStart];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cellRange2.Value = dsChiTieuDonVi[i][j].GIA_TRI;
                            cellRange2 = sheet.Cells[beginRow, colStart + 1];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = dsKHThucHienDonVi[i][j].GIA_TRI;

                            cellRange2 = sheet.Cells[beginRow, colStart + 2];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = dsChiTieuDonVi[i][j].GIA_TRI > 0 ?
                                Math.Round(((float)dsKHThucHienDonVi[i][j].GIA_TRI / dsChiTieuDonVi[i][j].GIA_TRI) * 100) : 0;
                        }
                        else
                        {
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cellRange2 = sheet.Cells[beginRow, colStart + 1];
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cellRange2 = sheet.Cells[beginRow, colStart + 2];
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        }
                    }
                    colStart = colStart + 3;
                }

                FileResult result = File(excelPackage.GetAsByteArray(), "application/octet-stream", "TinhHinhThucHienKeHoach" + namKH + ".xlsx");
                excelPackage.Dispose();

                return result;
            }
        }

        #region Khung chi tieu ke hoach nam
        public FileResult ExportKhungChiTieu(int namKHId)
        {
            NamKeHoachBO sqlNKH = new NamKeHoachBO();
            NamKeHoach namKH = sqlNKH.GetById(namKHId);
            ExcelPackage p = getExcelKhungChiTieu1(namKHId);

            FileResult result = File(p.GetAsByteArray(), "application/octet-stream",
                "KhungChiTieuKeHoach" + (namKH != null ? namKH.NAM_KE_HOACH.ToString() : string.Empty) + ".xlsx");

            return result;

        }

        public FileResult PrintKhungChiTieu(int namKHId)
        {
            ExcelPackage p = getExcelKhungChiTieu1(namKHId);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            //phunx1, 20160422: xu ly open pdf
            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A3;
            Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);

            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;

                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;


                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "BaoCaoTuan");
                    pdf.EndExport();
                    ms.Position = 0;
                    //return File(ms.ToArray(), "application/pdf");
                    return File(ms.ToArray(), "application/pdf");
                }
            }
        }

        public ExcelPackage getExcelKhungChiTieu(int namKHId)
        {
            ExportExcellBO sqlBO = new ExportExcellBO();
            NamKeHoachBO sqlNKH = new NamKeHoachBO();
            NamKeHoach namKH = sqlNKH.GetById(namKHId);
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuChinhThuc);
            List<ExportExcell> keHoachDieuChinhDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuDieuChinh);

            BinhQuanChiTieu(chiTieuChinhThucDHQG, namKHId);
            BinhQuanChiTieu(keHoachDieuChinhDHQG, namKHId);

            List<NhomDonVi> danhSachNhomDonVi = nhomdvbo.GetNhomDVByType(1);

            Dictionary<string, List<DonVi>> danhSachDonViTheoNhom = new Dictionary<string, List<DonVi>>();
            int count = danhSachNhomDonVi.Count;
            for (int i = 0; i < count; i++)
            {
                danhSachDonViTheoNhom.Add(danhSachNhomDonVi[i].TenNhomDonVi,
                    donvibo.GetAllDonViCoBanByNhom(danhSachNhomDonVi[i].NhomDonViId));
            }

            Dictionary<string, List<List<ExportExcell>>> data = new Dictionary<string, List<List<ExportExcell>>>();
            List<List<ExportExcell>> dataTheoDonVi = null;
            List<ExportExcell> dataDonVi = null;

            ExportExcellBO exBO = new ExportExcellBO();
            bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

            for (int i = 0; i < count; i++)
            {
                List<DonVi> danhSachDonVi = danhSachDonViTheoNhom[danhSachNhomDonVi[i].TenNhomDonVi];
                dataTheoDonVi = new List<List<ExportExcell>>();
                for (int j = 0; j < danhSachDonVi.Count; j++)
                {
                    if (coKeHoachDC)
                    {
                        dataDonVi = sqlBO.GetListChiTieuDonVi(namKHId, laChiTieuDieuChinh, danhSachDonVi[j].DonViId);
                    }
                    else
                    {
                        dataDonVi = sqlBO.GetListChiTieuDonVi(namKHId, laChiTieuChinhThuc, danhSachDonVi[j].DonViId);
                    }
                    dataTheoDonVi.Add(dataDonVi);
                }
                data.Add(danhSachNhomDonVi[i].TenNhomDonVi, dataTheoDonVi);
            }
            List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach(namKHId, 0);
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();

            #region header
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + "2016");
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;
            int tongSoDonVi = 12; //can lay dong listDonVi.Count
            var printArea = sheet.Cells[1, 1, 1000, tongSoDonVi * 3 + 7];
            printArea.Style.Font.SetFromFont(new Font("Arial", 10));

            // output a line for the headers
            sheet.Name = "Khung chỉ tiêu KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 1];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 2;
            var titleCell = sheet.Cells[startRowTile, 2, startRowTile, tongSoDonVi * 3 + 7];
            titleCell.Merge = true;
            titleCell.Value = "KHUNG CHỈ TIÊU KẾ HOẠCH NĂM " + "2016";//namKH;
            titleCell.Style.Font.SetFromFont(new Font("Arial", 12));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            int col = startCol;
            var cellRange1 = sheet.Cells[startRow, col];
            cellRange1.Value = "Chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            ExcelColumn colChiTieu = sheet.Column(startCol);
            colChiTieu.Style.WrapText = true;
            colChiTieu.Width = 60;
            col++;
            cellRange1 = sheet.Cells[startRow, col];
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            ExcelColumn colDonVi = sheet.Column(col);
            colDonVi.Style.WrapText = true;
            colDonVi.Width = 15;
            col++;
            cellRange1 = sheet.Cells[startRow, col];
            cellRange1.Value = "Phụ trách";
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            colDonVi = sheet.Column(col);
            colDonVi.Style.WrapText = true;
            colDonVi.Width = 15;

            col++;
            var cellRange2 = sheet.Cells[startRow, col, startRow, col + 1];
            cellRange2.Merge = true;
            cellRange2.Value = "ĐHQGHN";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange2 = sheet.Cells[startRow + 1, col];
            cellRange2.Value = "KH";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange2 = sheet.Cells[startRow + 1, col + 1];
            cellRange2.Value = "ĐC";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            col += 2;
            for (int i = 0; i < danhSachNhomDonVi.Count; i++)
            {
                List<DonVi> donvi = danhSachDonViTheoNhom[danhSachNhomDonVi[i].TenNhomDonVi];
                int start = col;
                for (int j = 0; j < donvi.Count; j++)
                {
                    cellRange2 = sheet.Cells[startRow + 1, col];
                    cellRange2.Value = donvi[j].TenVietTat;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange2.Style.Font.Bold = true;
                    col++;
                }
                cellRange2 = sheet.Cells[startRow, start, startRow, col - 1];
                cellRange2.Merge = true;
                cellRange2.Value = danhSachNhomDonVi[i].TenNhomDonVi;
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.WrapText = true;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            }
            #endregion

            int beginRow = startRow + 1;
            int beginCol = startCol;
            for (int i = 0; i < chiTieuChinhThucDHQG.Count; i++)
            {
                beginRow++;
                cellRange2 = sheet.Cells[beginRow, startCol];
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                if (chiTieuChinhThucDHQG[i].CAP == 1)
                {
                    cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU.ToUpper();
                }
                else cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;

                if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                    chiTieuChinhThucDHQG[i].CAP == 2)
                {
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Don vi tinh
                    cellRange2 = sheet.Cells[beginRow, startCol + 1];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Phu trach
                    cellRange2 = sheet.Cells[beginRow, startCol + 2];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    if (i < donViPhuTrach.Count)
                    {
                        if (donViPhuTrach[i].CAP == 2)
                        {
                            int chiTieuCap3Index = i + 1;
                            if (chiTieuCap3Index < donViPhuTrach.Count)
                                cellRange2.Value = (donViPhuTrach[chiTieuCap3Index].DON_VI ?? string.Empty);
                        }
                    }


                    //KH
                    cellRange2 = sheet.Cells[beginRow, startCol + 3];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //DC
                    cellRange2 = sheet.Cells[beginRow, startCol + 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                }

                if (chiTieuChinhThucDHQG[i].CAP > 2)
                {
                    cellRange2 = sheet.Cells[beginRow, startCol + 1];
                    cellRange2.Value = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //Phu trach
                    cellRange2 = sheet.Cells[beginRow, startCol + 2];
                    //cellRange2.Value = (donViPhuTrach[i].DON_VI ?? string.Empty);
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //KH
                    cellRange2 = sheet.Cells[beginRow, startCol + 3];
                    cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //DC
                    cellRange2 = sheet.Cells[beginRow, startCol + 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = keHoachDieuChinhDHQG[i].GIA_TRI;
                }
            }
            beginCol = startCol + 4;
            beginRow = startRow + 1;
            for (int i = 0; i < danhSachNhomDonVi.Count; i++)
            {
                dataTheoDonVi = data[danhSachNhomDonVi[i].TenNhomDonVi];
                for (int j = 0; j < dataTheoDonVi.Count; j++)
                {
                    beginCol++;
                    beginRow = startRow + 1;
                    for (int k = 0; k < dataTheoDonVi[j].Count; k++)
                    {
                        beginRow++;
                        cellRange2 = sheet.Cells[beginRow, beginCol];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        if (dataTheoDonVi[j][k].CAP < 3)
                        {
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else
                        {
                            cellRange2.Value = dataTheoDonVi[j][k].GIA_TRI;
                        }
                    }
                }

            }
            return excelPackage;
        }

        public ExcelPackage getExcelKhungChiTieu1(int namKHId)
        {
            ExportExcellBO sqlBO = new ExportExcellBO();
            NamKeHoachBO sqlNKH = new NamKeHoachBO();
            NamKeHoach namKH = sqlNKH.GetById(namKHId);
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuChinhThuc);
            List<ExportExcell> keHoachDieuChinhDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuDieuChinh);

            BinhQuanChiTieu(chiTieuChinhThucDHQG, namKHId);
            BinhQuanChiTieu(keHoachDieuChinhDHQG, namKHId);

            List<NhomDonVi> danhSachNhomDonVi = nhomdvbo.GetNhomDVByType(1);

            Dictionary<string, List<DonVi>> danhSachDonViTheoNhom = new Dictionary<string, List<DonVi>>();
            int count = danhSachNhomDonVi.Count;
            for (int i = 0; i < count; i++)
            {
                danhSachDonViTheoNhom.Add(danhSachNhomDonVi[i].TenNhomDonVi,
                    donvibo.GetAllDonViCoBanByNhom(danhSachNhomDonVi[i].NhomDonViId));
            }

            Dictionary<string, List<List<ExportExcell>>> data = new Dictionary<string, List<List<ExportExcell>>>();
            List<List<ExportExcell>> dataTheoDonVi = null;
            List<ExportExcell> dataDonVi = null;

            ExportExcellBO exBO = new ExportExcellBO();
            bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

            for (int i = 0; i < count; i++)
            {
                List<DonVi> danhSachDonVi = danhSachDonViTheoNhom[danhSachNhomDonVi[i].TenNhomDonVi];
                dataTheoDonVi = new List<List<ExportExcell>>();
                for (int j = 0; j < danhSachDonVi.Count; j++)
                {
                    if (coKeHoachDC)
                    {
                        dataDonVi = sqlBO.GetListChiTieuDonVi(namKHId, laChiTieuDieuChinh, danhSachDonVi[j].DonViId);
                    }
                    else
                    {
                        dataDonVi = sqlBO.GetListChiTieuDonVi(namKHId, laChiTieuChinhThuc, danhSachDonVi[j].DonViId);
                    }
                    dataTheoDonVi.Add(dataDonVi);
                }
                data.Add(danhSachNhomDonVi[i].TenNhomDonVi, dataTheoDonVi);
            }
            List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach(namKHId, 0);
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();

            #region header
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + "2016");
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;
            int tongSoDonVi = 12; //can lay dong listDonVi.Count

            var printArea = sheet.Cells[1, 1, 1000, tongSoDonVi * 3 + 7];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Khung chỉ tiêu KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 5];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 2;
            var titleCell = sheet.Cells[startRowTile, 2, startRowTile, tongSoDonVi * 3 + 7];
            titleCell.Merge = true;
            titleCell.Value = "KHUNG CHỈ TIÊU KẾ HOẠCH NĂM " + "2016";//namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Them cot STT
            //STT
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            var cellRange0 = sheet.Cells[startRow, 1, startRow + 1, 2];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;
            #endregion


            startCol = 3;
            int col = startCol;
            var cellRange1 = sheet.Cells[startRow, col, startRow + 1, col];
            cellRange1.Value = "Chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Merge = true;
            ExcelColumn colChiTieu = sheet.Column(startCol);
            colChiTieu.Style.WrapText = true;
            colChiTieu.Width = 60;
            col++;
            cellRange1 = sheet.Cells[startRow, col, startRow + 1, col];
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Merge = true;
            ExcelColumn colDonVi = sheet.Column(col);
            colDonVi.Style.WrapText = true;
            colDonVi.Width = 15;
            col++;
            cellRange1 = sheet.Cells[startRow, col, startRow + 1, col];
            cellRange1.Value = "Phụ trách";
            cellRange1.Merge = true;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            colDonVi = sheet.Column(col);
            colDonVi.Style.WrapText = true;
            colDonVi.Width = 15;

            col++;
            var cellRange2 = sheet.Cells[startRow, col, startRow, col + 1];
            cellRange2.Merge = true;
            cellRange2.Value = "ĐHQGHN";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange2 = sheet.Cells[startRow + 1, col];
            cellRange2.Value = "KH";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange2 = sheet.Cells[startRow + 1, col + 1];
            cellRange2.Value = "ĐC";
            cellRange2.Style.Font.Bold = true;
            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            col += 2;

            for (int i = 0; i < danhSachNhomDonVi.Count; i++)
            {
                List<DonVi> donvi = danhSachDonViTheoNhom[danhSachNhomDonVi[i].TenNhomDonVi];
                if (donvi.Count > 0)
                {
                    int start = col;
                    for (int j = 0; j < donvi.Count; j++)
                    {
                        cellRange2 = sheet.Cells[startRow + 1, col];
                        cellRange2.Value = donvi[j].TenVietTat;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.Font.Bold = true;
                        col++;
                    }
                    cellRange2 = sheet.Cells[startRow, start, startRow, col - 1];
                    cellRange2.Merge = true;
                    cellRange2.Value = danhSachNhomDonVi[i].TenNhomDonVi;
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Style.WrapText = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
                //else continue;

            }
            #endregion

            int beginRow = startRow + 1;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            bool cungDonViTinh = false;
            string currDonViTinh = "";
            double tongKH = 0;
            double tongDC = 0;
            string dvPhuTrachCTCap2 = "";
            int rowIndexCTCap2 = 0;

            for (int i = 0; i < chiTieuChinhThucDHQG.Count; i++)
            {
                if (chiTieuChinhThucDHQG[i].CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU.ToUpper();
                    cellRange2.Style.Font.Bold = true;
                    cellRange2.Merge = true;
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Don vi tinh
                    cellRange2 = sheet.Cells[beginRow, startCol + 1];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Phu trach
                    cellRange2 = sheet.Cells[beginRow, startCol + 2];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    if (i < donViPhuTrach.Count)
                    {
                        if (donViPhuTrach[i].CAP == 2)
                        {
                            int chiTieuCap3Index = i + 1;
                            if (chiTieuCap3Index < donViPhuTrach.Count)
                                cellRange2.Value = (donViPhuTrach[chiTieuCap3Index].DON_VI ?? string.Empty);
                        }
                    }


                    //KH
                    cellRange2 = sheet.Cells[beginRow, startCol + 3];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //DC
                    cellRange2 = sheet.Cells[beginRow, startCol + 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    #endregion
                }
                else if (chiTieuChinhThucDHQG[i].CAP == 2)
                {
                    #region Chi tieu cap 2
                    if (chiTieuChinhThucDHQG[i].LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        #region In Tong chi tieu (neu co)
                        if ((cungDonViTinh) && (currDonViTinh != "%"))
                        {
                            cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                            cellRange2.Value = currDonViTinh;

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                            cellRange2.Value = dvPhuTrachCTCap2;

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                            cellRange2.Value = tongKH;

                            cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                            cellRange2.Value = tongDC;
                            cungDonViTinh = false;
                        }
                        #endregion

                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        cungDonViTinh = true;
                        currDonViTinh = "";
                        tongKH = 0;
                        tongDC = 0;

                        chiTieuAo = false;
                        //STT
                        cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 1];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                        //Phu trach
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        //if (i < donViPhuTrach.Count)
                        //{
                        //    if (donViPhuTrach[i].CAP == 2)
                        //    {
                        //        int chiTieuCap3Index = i + 1;
                        //        if (chiTieuCap3Index < donViPhuTrach.Count)
                        //            cellRange2.Value = (donViPhuTrach[chiTieuCap3Index].DON_VI ?? string.Empty);
                        //    }
                        //}


                        //KH
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Numberformat.Format = "###,###,###";
                        //DC
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Numberformat.Format = "###,###,###";

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                    #endregion
                }
                else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                {
                    #region Chi tieu cap 3
                    beginRow++;

                    if (chiTieuAo)
                    {
                        //STT
                        cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        if (sttLevel2 == 1)
                        {
                            dvPhuTrachCTCap2 = donViPhuTrach[i].DON_VI;
                            currDonViTinh = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                            cungDonViTinh = true;
                            tongKH = tongKH + chiTieuChinhThucDHQG[i].GIA_TRI;
                            tongDC = tongDC + keHoachDieuChinhDHQG[i].GIA_TRI;
                        }
                        else
                        {
                            if (cungDonViTinh)
                            {
                                if ((currDonViTinh != null) && (chiTieuChinhThucDHQG[i].DON_VI_TINH != null) &&
                                    (currDonViTinh.Trim().ToUpper() == chiTieuChinhThucDHQG[i].DON_VI_TINH.Trim().ToUpper()))
                                {
                                    cungDonViTinh = true;
                                    tongKH = tongKH + chiTieuChinhThucDHQG[i].GIA_TRI;
                                    tongDC = tongDC + keHoachDieuChinhDHQG[i].GIA_TRI;
                                }
                                else
                                {
                                    cungDonViTinh = false;
                                }
                            }

                            if (dvPhuTrachCTCap2 != donViPhuTrach[i].DON_VI) dvPhuTrachCTCap2 = "";
                        }

                        //STT
                        cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }
                    //ten chi tieu
                    cellRange2 = sheet.Cells[beginRow, startCol];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = chiTieuChinhThucDHQG[i].TEN_CHI_TIEU;

                    //Don vi tinh
                    cellRange2 = sheet.Cells[beginRow, startCol + 1];
                    cellRange2.Value = chiTieuChinhThucDHQG[i].DON_VI_TINH;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    //Phu trach
                    cellRange2 = sheet.Cells[beginRow, startCol + 2];
                    cellRange2.Value = (donViPhuTrach[i].DON_VI ?? string.Empty);
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //KH
                    cellRange2 = sheet.Cells[beginRow, startCol + 3];
                    cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Numberformat.Format = "###,###,###";
                    //DC
                    cellRange2 = sheet.Cells[beginRow, startCol + 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Value = keHoachDieuChinhDHQG[i].GIA_TRI;
                    cellRange2.Style.Numberformat.Format = "###,###,###";
                    #endregion
                }
            }
            #region In Tong chi tieu ĐHQG (neu co)
            if ((cungDonViTinh) && (currDonViTinh != "%"))
            {
                cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                cellRange2.Value = currDonViTinh;

                cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                cellRange2.Value = dvPhuTrachCTCap2;

                cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                cellRange2.Value = tongKH;

                cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                cellRange2.Value = tongDC;
                cungDonViTinh = false;
            }
            #endregion

            beginCol = startCol + 4;
            beginRow = startRow + 1;
            cungDonViTinh = false;
            for (int i = 0; i < danhSachNhomDonVi.Count; i++)
            {
                dataTheoDonVi = data[danhSachNhomDonVi[i].TenNhomDonVi];
                for (int j = 0; j < dataTheoDonVi.Count; j++)
                {
                    beginCol++;
                    beginRow = startRow + 1;
                    for (int k = 0; k < dataTheoDonVi[j].Count; k++)
                    {
                        #region Sum up
                        if (dataTheoDonVi[j][k].CAP == 2)
                        {
                            //if (dataTheoDonVi[j][k].LA_CHI_TIEU_AO == 0)
                            {
                                #region In Tong chi tieu don vi (neu co)
                                if ((cungDonViTinh) && (currDonViTinh != "%"))
                                {
                                    cellRange2 = sheet.Cells[rowIndexCTCap2, beginCol];
                                    cellRange2.Value = tongKH;
                                    cungDonViTinh = false;
                                }
                                #endregion

                                tongKH = 0;
                                currDonViTinh = "";
                                rowIndexCTCap2 = beginRow + 1;
                                cungDonViTinh = true;
                                currDonViTinh = "";
                                tongKH = 0;
                            }
                            sttLevel2 = 1;
                        }
                        else if (dataTheoDonVi[j][k].CAP > 2)
                        {

                            if (sttLevel2 == 1)
                            {
                                currDonViTinh = dataTheoDonVi[j][k].DON_VI_TINH;
                                cungDonViTinh = true;
                                tongKH = dataTheoDonVi[j][k].GIA_TRI;
                            }
                            else
                            {
                                if (cungDonViTinh)
                                {
                                    if ((currDonViTinh != null) && (dataTheoDonVi[j][k].DON_VI_TINH != null) &&
                                        (currDonViTinh.Trim().ToUpper() == dataTheoDonVi[j][k].DON_VI_TINH.Trim().ToUpper()))
                                    {
                                        cungDonViTinh = true;
                                        tongKH = tongKH + dataTheoDonVi[j][k].GIA_TRI;

                                    }
                                    else
                                    {
                                        cungDonViTinh = false;
                                    }
                                }
                            }
                            sttLevel2 = sttLevel2 + 1;
                        }
                        #endregion

                        if (dataTheoDonVi[j][k].LA_CHI_TIEU_AO == 0)
                        {
                            //=============

                            //==============

                            beginRow++;
                            cellRange2 = sheet.Cells[beginRow, beginCol];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                            if (dataTheoDonVi[j][k].CAP < 3)
                            {
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            }
                            else
                            {
                                cellRange2.Value = dataTheoDonVi[j][k].GIA_TRI;
                            }
                        }
                    }
                    #region In Tong chi tieu don vi (neu co)
                    if ((cungDonViTinh) && (currDonViTinh != "%"))
                    {
                        cellRange2 = sheet.Cells[rowIndexCTCap2, beginCol];
                        cellRange2.Value = tongKH;
                        cungDonViTinh = false;
                    }
                    #endregion
                }

            }


            return excelPackage;
        }
        #endregion

        #region Chi tieu rieng
        public FileResult ExportChiTieuRieng(int namKHId, int donViId, string namKH)
        {
            try
            {
                ExcelPackage excelPackage = getExcelChiTieuRieng(namKHId, donViId, namKH);
                FileResult result = File(excelPackage.GetAsByteArray(), "application/octet-stream", "ChiTieuRieng" + "2016" + ".xlsx");
                return result;
            }
            catch
            {
                return null;
            }
        }

        public FileResult PrintChiTieuRieng(int namKHId, int donViId, string namKH)
        {
            ExcelPackage p = getExcelChiTieuRieng(namKHId, donViId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "KeHoachThucHienDHQG");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;
        }

        public ExcelPackage getExcelChiTieuRieng(int namKHId, int donViId, string namKH)
        {
            try
            {
                int nhomDonViId = 0;
                ExportExcellBO sqlBO = new ExportExcellBO();
                int laChiTieuChinhThuc = 31;
                int laChiTieuDieuChinh = 61;
                DonVi listDonVi = donvibo.GetById(donViId);

                ExportExcellBO exBO = new ExportExcellBO();
                bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);

                List<ExportExcell> chiTieuChinhThucDHQG;
                if (coKeHoachDC)
                {
                    chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG_TheoNhom(namKHId, laChiTieuDieuChinh, donViId, nhomDonViId);
                }
                else
                {
                    chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, nhomDonViId);
                }

                List<ExportExcell> chiTieuDonVi;
                if (coKeHoachDC)
                {
                    chiTieuDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, listDonVi.DonViId, nhomDonViId);
                }
                else
                {
                    chiTieuDonVi = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, listDonVi.DonViId, nhomDonViId);
                }
                List<ExportExcell> keHoachThucHienDonVi = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, listDonVi.DonViId, nhomDonViId);

                List<ExportExcell> banPhuTrach = sqlBO.GetListBanPhuTrach_TheoDonVi(namKHId, 0, donViId);

                List<ExportExcell> chiTieuRiengDauNam = sqlBO.GetListChiTieuRiengDonVi_KeHoach(namKHId, listDonVi.DonViId, 21);
                List<ExportExcell> chiTieuRiengThucHien = sqlBO.GetListChiTieuRiengDonVi_KeHoachThucHien(namKHId, listDonVi.DonViId);

                Color backGroundColor = Color.FromArgb(208, 206, 206);

                var excelPackage = new ExcelPackage();
                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                string sheetTitle = (listDonVi != null ? listDonVi.TenVietTat + namKH : string.Empty);
                var sheet = excelPackage.Workbook.Worksheets.Add(sheetTitle + " - " + namKH);

                #region Set Margion
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;
                sheet.PrinterSettings.FitToWidth = 1;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 5;
                sheet.Column(2).Width = 40;
                sheet.Column(3).Width = 10;
                sheet.Column(4).Width = 10;
                sheet.Column(5).Width = 8;
                sheet.Column(6).Width = 8;
                sheet.Column(7).Width = 8;
                sheet.Column(8).Width = 8;
                #endregion

                var printArea = sheet.Cells[1, 1, 1000, 10];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));
                //var printArea = sheet.Cells[1, 1, 1000, listDonVi.Count * 3 + 7];
                //printArea.Style.Font.SetFromFont(new Font("Arial", 10));

                // output a line for the headers
                sheet.Name = "Chi tiêu " + namKH;
                int startRowLogo = 1;
                int startColumnLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, startColumnLogo, startRowLogo, startColumnLogo + 5];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.Bold = true;
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                startRowLogo += 1;

                int startRowTile = startRowLogo + 1;
                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 7];
                titleCell.Merge = true;
                titleCell.Value = "BÁO CÁO TIẾN ĐỘ THỰC HIỆN KẾ HOẠCH NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Thong tin moc kiem soat
                int namkh = 0;
                if (!string.IsNullOrEmpty(namKH))
                {
                    try
                    {
                        namkh = System.Convert.ToInt32(namKH);
                    }
                    catch { }
                }
                MocKiemSoatBO mc = new MocKiemSoatBO();
                MocKiemSoat mks = mc.GetMocKSIsActive(namkh);

                string kyBC = string.Empty;
                if (mks != null)
                {
                    kyBC = mks.MOC_KIEM_SOAT;
                }
                startRowTile += 1;
                var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 7];
                kyBaoCaoCell.Merge = true;
                kyBaoCaoCell.Value = listDonVi != null ? listDonVi.TenDonVi.ToUpper() : string.Empty;// "Kỳ báo cáo: " + kyBC;
                kyBaoCaoCell.Style.Font.Bold = true;
                kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                startRow = startRowTile + 1;
                var cellRange1 = sheet.Cells[startRow, 1, startRow + 1, 1];
                cellRange1.Merge = true;
                cellRange1.Value = "A";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                ExcelColumn colNhomCT = sheet.Column(1);
                colNhomCT.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Các Chỉ tiêu Chung".ToUpper();
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                ExcelColumn colChiTieu = sheet.Column(startCol);
                colChiTieu.Style.WrapText = true;
                //colChiTieu.Width = 60;

                #region Header
                cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "ĐV phân bổ và giám sát";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Mức chỉ tiêu năm " + namKH + " của toàn ĐHQGHN";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                // Mức chỉ tiêu năm 2016 của đơn vị 
                cellRange1 = sheet.Cells[startRow, startCol + 5, startRow + 1, startCol + 5];
                cellRange1.Merge = true;
                cellRange1.Value = "Mức chỉ tiêu năm " + namKH + " của đơn vị";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 6, startRow, startCol + 7];
                cellRange1.Merge = true;
                cellRange1.Style.WrapText = true;
                cellRange1.Value = "Kết quả thực hiện";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;

                cellRange1 = sheet.Cells[startRow + 1, startCol + 6];
                cellRange1.Value = "Thực hiện " + kyBC;
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow + 1, startCol + 7];
                cellRange1.Value = "Tỉ lệ hoàn thành";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;
                #endregion

                #region Chi tieu chung
                startCol = 2;
                int beginRow = startRow + 1;
                int beginCol = startCol;

                ExcelRange banPhuTrachCell = null;
                ExcelRange cellRange = null;
                for (int i = 0; i < chiTieuChinhThucDHQG.Count; i++)
                {
                    beginRow++;

                    var cellSTT = sheet.Cells[beginRow, 1];
                    cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellSTT.Value = (i + 1).ToString();

                    cellRange = sheet.Cells[beginRow, startCol + 3];
                    cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    banPhuTrachCell = sheet.Cells[beginRow, startCol + 1];
                    banPhuTrachCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    banPhuTrachCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    //banPhuTrachCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                        chiTieuChinhThucDHQG[i].CAP == 2)
                    {
                        cellRange.Style.Font.Bold = true;
                        cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        banPhuTrachCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        banPhuTrachCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        if (i < banPhuTrach.Count)
                        {
                            banPhuTrachCell.Value = banPhuTrach[i].DON_VI;
                            if (banPhuTrach[i].CAP == 2)
                            {
                                int chiTieuCap3 = i + 1;
                                if (chiTieuCap3 < banPhuTrach.Count)
                                {
                                    banPhuTrachCell.Value = banPhuTrach[chiTieuCap3].DON_VI;
                                }
                            }
                        }

                    }

                    if (chiTieuChinhThucDHQG[i].CAP > 2)
                    {
                        cellRange.Value = chiTieuChinhThucDHQG[i].GIA_TRI;
                        //banPhuTrachCell.Value = banPhuTrach[i].DON_VI;
                    }
                }
                cellRange = sheet.Cells[startRow + 1, startCol, beginRow, startCol + 1];
                cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                //cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ExcelColumn colDonViPhuTrach = sheet.Column(startCol + 1);
                colDonViPhuTrach.Style.WrapText = true;
                //colChiTieu.Width = 50;

                int colStart = startCol;
                beginRow = startRow + 1;

                int len = chiTieuDonVi.Count;
                beginRow = startRow + 1;
                ExcelRange celDonViTinh = null;
                for (int j = 0; j < len; j++)
                {
                    beginRow++;
                    cellRange = sheet.Cells[beginRow, startCol];
                    cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    celDonViTinh = sheet.Cells[beginRow, startCol + 2];
                    cellRange.Value = chiTieuDonVi[j].TEN_CHI_TIEU;
                    cellRange.Style.WrapText = true;
                    celDonViTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    if (chiTieuDonVi[j].CAP <= 2)
                    {
                        //Don vi tinh
                        celDonViTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        celDonViTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        celDonViTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        celDonViTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Ten chi tieu
                        cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange.Style.WrapText = true;

                        cellRange = sheet.Cells[beginRow, colStart + 4];
                        cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //KH
                        cellRange = sheet.Cells[beginRow, colStart + 5];
                        cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                        cellRange = sheet.Cells[beginRow, colStart + 6];
                        cellRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    }
                    else if (chiTieuDonVi[j].CAP > 2)
                    {
                        //cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                        celDonViTinh.Value = chiTieuDonVi[j].DON_VI_TINH;
                        celDonViTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        celDonViTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        celDonViTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Chỉ tiêu đơn vị
                        cellRange = sheet.Cells[beginRow, colStart + 4];
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange.Value = chiTieuDonVi[j].GIA_TRI;
                        cellRange.Style.Numberformat.Format = "###,###,###";

                        //Thực hiện 
                        cellRange = sheet.Cells[beginRow, colStart + 5];
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange.Value = keHoachThucHienDonVi[j].GIA_TRI;
                        cellRange.Style.Numberformat.Format = "###,###,###";

                        //Tỉ lệ hoàn thành
                        cellRange = sheet.Cells[beginRow, colStart + 6];
                        cellRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange.Value = chiTieuDonVi[j].GIA_TRI > 0 ?
                            Math.Round(((float)keHoachThucHienDonVi[j].GIA_TRI / chiTieuDonVi[j].GIA_TRI) * 100, 2) : 0;
                    }
                }
                #endregion

                int startRowChiTieuRieng = beginRow + 1;

                #region Chi tieu rieng
                startCol = 2;
                if (chiTieuRiengDauNam.Count > 0)
                {
                    var cellNhomCTR = sheet.Cells[startRowChiTieuRieng, 1, startRowChiTieuRieng, 1];
                    cellNhomCTR.Merge = true;
                    cellNhomCTR.Value = "B";
                    cellNhomCTR.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellNhomCTR.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellNhomCTR.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellNhomCTR.Style.Font.Bold = true;
                    ExcelColumn colNhomCTR = sheet.Column(1);
                    colNhomCTR.Style.WrapText = true;

                    ExcelRange cotChiTieu = sheet.Cells[startRowChiTieuRieng, startCol];
                    ExcelRange cotDonviTinh = sheet.Cells[startRowChiTieuRieng, startCol + 1, startRowChiTieuRieng, startCol + 2];
                    ExcelRange cotGiaTriChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 3, startRowChiTieuRieng, startCol + 4];
                    ExcelRange cotThucHien = sheet.Cells[startRowChiTieuRieng, startCol + 5];
                    ExcelRange cotTiLe = sheet.Cells[startRowChiTieuRieng, startCol + 6];

                    cotChiTieu.Value = "CÁC CHỈ TIÊU ĐẶC THÙ CỦA ĐƠN VỊ";
                    cotChiTieu.Style.Font.Bold = true;
                    cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotChiTieu.Style.WrapText = true;
                    cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotDonviTinh.Merge = true;
                    cotDonviTinh.Value = "Đơn vị tính";
                    cotDonviTinh.Style.Font.Bold = true;
                    cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotDonviTinh.Style.WrapText = true;
                    cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotGiaTriChiTieu.Merge = true;
                    cotGiaTriChiTieu.Value = "Mức chỉ tiêu năm " + namKH + " đơn vị đăng ký";
                    cotGiaTriChiTieu.Style.Font.Bold = true;
                    cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotGiaTriChiTieu.Style.WrapText = true;
                    cotGiaTriChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotThucHien.Style.Font.Bold = true;
                    cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotThucHien.Style.WrapText = true;

                    cotTiLe.Style.Font.Bold = true;
                    cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotTiLe.Style.WrapText = true;
                    beginRow = startRowChiTieuRieng;

                    for (int i = 0; i < chiTieuRiengDauNam.Count; i++)
                    {
                        beginRow++;
                        var cellSTT = sheet.Cells[beginRow, 1];
                        cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellSTT.Value = (i + 1).ToString();

                        cotChiTieu = sheet.Cells[beginRow, startCol];
                        cotDonviTinh = sheet.Cells[beginRow, startCol + 1, beginRow, startCol + 2];
                        cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                        cotThucHien = sheet.Cells[beginRow, startCol + 5];
                        cotTiLe = sheet.Cells[beginRow, startCol + 6];

                        cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                        cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotChiTieu.Style.WrapText = true;
                        cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cotDonviTinh.Merge = true;
                        cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.WrapText = true;
                        cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                        cotGiaTriChiTieu.Merge = true;
                        cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotGiaTriChiTieu.Style.WrapText = true;
                        cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotThucHien.Style.WrapText = true;
                        cotThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotTiLe.Style.WrapText = true;
                        cotTiLe.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if (chiTieuRiengDauNam[i].CAP <= 2)
                        {
                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else
                        {
                            //cotDonviTinh.Value = (chiTieuRiengDauNam[i].DON_VI_TINH.ToUpper() == "NA" ?
                            //    string.Empty : chiTieuRiengDauNam[i].DON_VI_TINH);
                            cotDonviTinh.Value = chiTieuRiengDauNam[i].DON_VI_TINH;

                            cotGiaTriChiTieu.Value = chiTieuRiengDauNam[i].GIA_TRI;
                            cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";

                            cotThucHien.Value = chiTieuRiengThucHien[i].GIA_TRI;
                            cotThucHien.Style.Numberformat.Format = "###,###,###";

                            cotTiLe.Value = (chiTieuRiengDauNam[i].GIA_TRI > 0 ?
                                Math.Round((chiTieuRiengThucHien[i].GIA_TRI / chiTieuRiengDauNam[i].GIA_TRI) * 100, 2) : 0);
                        }
                    }
                }
                #endregion

                //FileResult result = File(excelPackage.GetAsByteArray(), "application/octet-stream", "ChiTieuRieng" + "2016" + ".xlsx");

                return excelPackage;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        private List<double> TongThucHien_TheoNhom(int namKHId, int nhomDonViId)
        {
            ExportExcellBO sqlBO = new ExportExcellBO();
            List<DonVi> toanBoDonVi = donvibo.GetAllDonViCoBanByNhom(nhomDonViId);
            List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
            int laChiTieuChinhThuc = 31;
            int len = toanBoDonVi.Count;
            for (int i = 0; i < len; i++)
            {
                dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, toanBoDonVi[i].DonViId, nhomDonViId));
            }

            List<double> tongThucHien = new List<double>();
            int count = dsKHThucHienDonVi.Count;
            double tongTungChiTieu = 0;
            len = 0;
            if (dsKHThucHienDonVi.Count > 0)
            {
                len = dsKHThucHienDonVi[0].Count;
            }

            for (int i = 0; i < len; i++)
            {
                tongTungChiTieu = 0;
                for (int j = 0; j < dsKHThucHienDonVi.Count; j++)
                {
                    if (dsKHThucHienDonVi[j][i].CAP == 1 || dsKHThucHienDonVi[j][i].CAP == 2)
                        break;
                    tongTungChiTieu += dsKHThucHienDonVi[j][i].GIA_TRI;
                }
                tongThucHien.Add(tongTungChiTieu);
            }

            return tongThucHien;
        }

        private List<double> TongThucHien(int namKHId, int mocKS)
        {
            ExportExcellBO sqlBO = new ExportExcellBO();
            List<DonVi> toanBoDonVi = donvibo.GetAll();
            List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
            int laChiTieuChinhThuc = 31;
            int len = toanBoDonVi.Count;
            for (int i = 0; i < len; i++)
            {
                dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi2(namKHId, laChiTieuChinhThuc, toanBoDonVi[i].DonViId, mocKS));
            }

            List<double> tongThucHien = new List<double>();
            int count = dsKHThucHienDonVi.Count;
            double tongTungChiTieu = 0;
            len = 0;
            if (dsKHThucHienDonVi.Count > 0)
            {
                len = dsKHThucHienDonVi[0].Count;
            }

            for (int i = 0; i < len; i++)
            {
                tongTungChiTieu = 0;
                for (int j = 0; j < dsKHThucHienDonVi.Count; j++)
                {
                    if (dsKHThucHienDonVi[j][i].CAP == 1 || dsKHThucHienDonVi[j][i].CAP == 2 ||
                        dsKHThucHienDonVi[j][i].CHO_PHEP_TINH_TONG == 0)
                        break;
                    tongTungChiTieu += dsKHThucHienDonVi[j][i].GIA_TRI;
                }
                tongThucHien.Add(tongTungChiTieu);
            }

            return tongThucHien;
        }

        private List<double> TongThucHien(int namKHId)
        {
            ExportExcellBO sqlBO = new ExportExcellBO();
            List<DonVi> toanBoDonVi = donvibo.GetAll();
            List<List<ExportExcell>> dsKHThucHienDonVi = new List<List<ExportExcell>>();
            int laChiTieuChinhThuc = 31;
            int len = toanBoDonVi.Count;
            for (int i = 0; i < len; i++)
            {
                dsKHThucHienDonVi.Add(sqlBO.GetListKeHoachThucHienDonVi(namKHId, laChiTieuChinhThuc, toanBoDonVi[i].DonViId));
            }

            List<double> tongThucHien = new List<double>();
            int count = dsKHThucHienDonVi.Count;
            double tongTungChiTieu = 0;
            len = 0;
            if (dsKHThucHienDonVi.Count > 0)
            {
                len = dsKHThucHienDonVi[0].Count;
            }

            for (int i = 0; i < len; i++)
            {
                tongTungChiTieu = 0;
                for (int j = 0; j < dsKHThucHienDonVi.Count; j++)
                {
                    if (dsKHThucHienDonVi[j][i].CAP == 1 || dsKHThucHienDonVi[j][i].CAP == 2 ||
                        dsKHThucHienDonVi[j][i].CHO_PHEP_TINH_TONG == 0)
                        break;
                    tongTungChiTieu += dsKHThucHienDonVi[j][i].GIA_TRI;
                }
                tongThucHien.Add(tongTungChiTieu);
            }

            return tongThucHien;
        }

        private void BinhQuanChiTieu(List<ExportExcell> data, int namKhId)
        {
            if (data == null)
            {
                new ArgumentNullException("");
            }
            ExportExcellBO sqlExportExcell = new ExportExcellBO();
            int len = data.Count;
            for (int i = 0; i < len; i++)
            {
                if (data[i].DON_VI_TINH.Contains("%"))
                {
                    if (data[i].GIA_TRI > 0)
                    {
                        int soLuongDV = sqlExportExcell.GetSoLuongDonViSuDungChiTieu(namKhId, data[i].CHI_TIEU_ID);
                        if (soLuongDV > 0)
                        {
                            data[i].GIA_TRI = Math.Round((data[i].GIA_TRI / (float)soLuongDV), 2);
                        }
                    }
                }
            }
        }

        #region Bao cao ban hanh dieu chinh
        #region Sheet 1
        public FileResult ExporBanHanhDieuChinhDHQG(int namKHId, string namKH)
        {
            ExcelPackage ep = getExcelPackBanHanhDieuChinhDHQG(namKHId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDHQG.xlsx");
            return result;
        }

        public FileResult PrintBanHanhDieuChinhDHQG(int namKHId, string namKH)
        {
            ExcelPackage p = getExcelPackBanHanhDieuChinhDHQG(namKHId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDHQG");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        public FileResult ExporBanHanhDieuChinhDHQG_ChuaCT(int namKHId, string namKH)
        {
            ExcelPackage ep = getExcelPackBanHanhDieuChinhDHQG_ChuaChinhThuc(namKHId, namKH); //this.getExcelPackBanHanhDieuChinhDHQG(namKHId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDHQG.xlsx");
            return result;
        }

        public FileResult PrintBanHanhDieuChinhDHQG_ChuaCT(int namKHId, string namKH)
        {
            ExcelPackage p = getExcelPackBanHanhDieuChinhDHQG_ChuaChinhThuc(namKHId, namKH); //getExcelPackBanHanhDieuChinhDHQG(namKHId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDHQG");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackBanHanhDieuChinhDHQG1(int namKHId, string namKH)
        {
            string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_DHQG.xlsx");
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = p.Workbook.Worksheets[1];

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                var cellRange = ws.Cells[4, 1, 4, 6];
                cellRange.Merge = true;
                cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange.Value = "ĐIỀU CHỈNH CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;


                KeHoachBO khBO = new KeHoachBO();
                List<DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDHQG(namKHId, 31);

                int stt = 1;
                startRow = 9;

                foreach (DIEU_CHINH_GIUA_NAM item in lstDC)
                {
                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 2].Value = item.TEN_CHI_TIEU;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Value = item.DV_PHU_TRACH;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    ws.Cells[startRow, 4].Value = item.DON_VI_TINH;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    ws.Cells[startRow, 5].Value = item.KE_HOACH;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 6].Value = item.DIEU_CHINH;
                    ws.Cells[startRow, 6].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 6].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 6].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 6].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return p;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private ExcelPackage getExcelPackBanHanhDieuChinhDHQG(int namKHId, string namKH)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            KeHoachBO khBO = new KeHoachBO();
            List<DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDHQG(namKHId, 31);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            //using (var excelPackage = new ExcelPackage())
            //{
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, 1000, 7];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];


            titleCell.Merge = true;
            titleCell.Value = "ĐIỀU CHỈNH CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin don vi bao cao
            startRowTile += 1;
            var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 7];
            donViThucHien.Merge = true;
            donViThucHien.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViThucHien.Style.Font.Bold = true;
            donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 44;
            sheet.Column(4).Width = 12;
            sheet.Column(5).Width = 12;
            sheet.Column(6).Width = 10;
            sheet.Column(7).Width = 10;

            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;

            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Tên chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;
            sheet.Row(startRow).Height = 40;

            cellRange1 = sheet.Cells[startRow, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 5];
            cellRange1.Merge = true;
            cellRange1.Value = "KH đầu năm";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 6];
            cellRange1.Merge = true;
            cellRange1.Value = "KH sau ĐC";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            int beginRow = startRow;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            bool cungDonViTinh = false;
            string currDonViTinh = "";
            double tongKH = 0;
            double tongDC = 0;
            //double tyLeDC = 0;
            string dvPhuTrachCTCap2 = "";
            int rowIndexCTCap2 = 0;
            foreach (DIEU_CHINH_GIUA_NAM item in lstDC)
            {
                if (item.CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                    //DV giam sat
                    cellRange2 = sheet.Cells[beginRow, 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Đơn vị tính
                    cellRange2 = sheet.Cells[beginRow, 5];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //KH đầu năm
                    cellRange2 = sheet.Cells[beginRow, 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //KH sau ĐC
                    cellRange2 = sheet.Cells[beginRow, 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    #endregion
                }
                else if (item.CAP == 2)
                {
                    if (item.LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        #region In Tong chi tieu (neu co)
                        //if (cungDonViTinh)
                        //{
                        //    var cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 4];
                        //    cellRangeSumUp.Value = dvPhuTrachCTCap2;

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 5];
                        //    cellRangeSumUp.Value = currDonViTinh;

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 6];
                        //    cellRangeSumUp.Value = tongKH;
                        //    if (tongKH % 1 == 0)
                        //    {
                        //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
                        //    }
                        //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 7];
                        //    cellRangeSumUp.Value = tongDC;
                        //    if (tongDC % 1 == 0)
                        //    {
                        //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
                        //    }
                        //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";
                        //    cungDonViTinh = false;
                        //}
                        #endregion

                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        cungDonViTinh = true;
                        currDonViTinh = "";
                        tongKH = 0;
                        tongDC = 0;

                        chiTieuAo = false;
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, 3];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = item.TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH đầu năm
                        cellRange2 = sheet.Cells[beginRow, 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH sau ĐC
                        cellRange2 = sheet.Cells[beginRow, 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                }
                else
                {
                    beginRow++;
                    if (chiTieuAo)
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        if (sttLevel2 == 1)
                        {
                            dvPhuTrachCTCap2 = item.DV_PHU_TRACH;
                            currDonViTinh = item.DON_VI_TINH;
                            cungDonViTinh = true;
                            tongKH = tongKH + item.KE_HOACH;
                            tongDC = tongDC + item.DIEU_CHINH;
                        }
                        else
                        {
                            if (cungDonViTinh)
                            {
                                if ((currDonViTinh != null) && (item.DON_VI_TINH != null) &&
                                    (currDonViTinh.Trim().ToUpper() == item.DON_VI_TINH.Trim().ToUpper()))
                                {
                                    cungDonViTinh = true;
                                    tongKH = tongKH + item.KE_HOACH;
                                    tongDC = tongDC + item.DIEU_CHINH;
                                }
                                else
                                {
                                    cungDonViTinh = false;
                                }
                            }

                            if (dvPhuTrachCTCap2 != item.DV_PHU_TRACH) dvPhuTrachCTCap2 = "";
                        }

                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                    cellRange3.Value = item.TEN_CHI_TIEU;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //Don vi giam sat
                    cellRange3 = sheet.Cells[beginRow, startCol + 3];
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.WrapText = true;
                    cellRange3.Value = item.DV_PHU_TRACH;

                    //Don vi tinh
                    cellRange3 = sheet.Cells[beginRow, startCol + 4];
                    cellRange3.Value = item.DON_VI_TINH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.WrapText = true;

                    //KH đầu năm
                    cellRange3 = sheet.Cells[beginRow, startCol + 5];
                    cellRange3.Value = item.KE_HOACH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    if ((item.KE_HOACH < 1) && (item.KE_HOACH > 0))
                    {
                        cellRange3.Style.Numberformat.Format = "0.##";
                    }
                    else if (item.KE_HOACH % 1 == 0)
                    {
                        cellRange3.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange3.Style.Numberformat.Format = "###,###,###.##";

                    //KH sau ĐC
                    cellRange3 = sheet.Cells[beginRow, startCol + 6];
                    cellRange3.Value = item.DIEU_CHINH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    if ((item.DIEU_CHINH < 1) && (item.DIEU_CHINH > 0))
                    {
                        cellRange3.Style.Numberformat.Format = "0.##";
                    }
                    else if (item.DIEU_CHINH % 1 == 0)
                    {
                        cellRange3.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange3.Style.Numberformat.Format = "###,###,###.##";

                }
            }
            #region In Tong chi tieu (neu co)
            //if (cungDonViTinh)
            //{
            //    var cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 4];
            //    cellRangeSumUp.Value = dvPhuTrachCTCap2;

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 5];
            //    cellRangeSumUp.Value = currDonViTinh;

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 6];
            //    cellRangeSumUp.Value = tongKH;
            //    if (tongKH % 1 == 0)
            //    {
            //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
            //    }
            //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 7];
            //    cellRangeSumUp.Value = tongDC;
            //    if (tongDC % 1 == 0)
            //    {
            //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
            //    }
            //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";
            //    cungDonViTinh = false;
            //}
            #endregion

            return excelPackage;

        }

        private ExcelPackage getExcelPackBanHanhDieuChinhDHQG_ChuaChinhThuc(int namKHId, string namKH)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            KeHoachBO khBO = new KeHoachBO();
            List<DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDHQG_ChuaCT(namKHId); //khBO.GetDieuChinhDHQG(namKHId, 31);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            //using (var excelPackage = new ExcelPackage())
            //{
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, 1000, 7];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];


            titleCell.Merge = true;
            titleCell.Value = "ĐIỀU CHỈNH CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin don vi bao cao
            startRowTile += 1;
            var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 7];
            donViThucHien.Merge = true;
            donViThucHien.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViThucHien.Style.Font.Bold = true;
            donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 44;
            sheet.Column(4).Width = 12;
            sheet.Column(5).Width = 12;
            sheet.Column(6).Width = 10;
            sheet.Column(7).Width = 10;

            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;

            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Tên chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;
            sheet.Row(startRow).Height = 40;

            cellRange1 = sheet.Cells[startRow, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 5];
            cellRange1.Merge = true;
            cellRange1.Value = "KH đầu năm";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 6];
            cellRange1.Merge = true;
            cellRange1.Value = "KH sau ĐC";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            int beginRow = startRow;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            double giaTriDieuChinh = 0;
            bool cungDonViTinh = false;
            string currDonViTinh = "";
            double tongKH = 0;
            double tongDC = 0;
            //double tyLeDC = 0;
            string dvPhuTrachCTCap2 = "";
            int rowIndexCTCap2 = 0;
            foreach (DIEU_CHINH_GIUA_NAM item in lstDC)
            {
                if (item.CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                    //DV giam sat
                    cellRange2 = sheet.Cells[beginRow, 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Đơn vị tính
                    cellRange2 = sheet.Cells[beginRow, 5];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //KH đầu năm
                    cellRange2 = sheet.Cells[beginRow, 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //KH sau ĐC
                    cellRange2 = sheet.Cells[beginRow, 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    #endregion
                }
                else if (item.CAP == 2)
                {
                    #region Chi tieu cap 2
                    if (item.LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        #region In Tong chi tieu (neu co)
                        //if (cungDonViTinh)
                        //{
                        //    var cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 4];
                        //    cellRangeSumUp.Value = dvPhuTrachCTCap2;

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 5];
                        //    cellRangeSumUp.Value = currDonViTinh;

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 6];
                        //    cellRangeSumUp.Value = tongKH;
                        //    if (tongKH % 1 == 0)
                        //    {
                        //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
                        //    }
                        //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 7];
                        //    cellRangeSumUp.Value = tongDC;
                        //    if (tongDC % 1 == 0)
                        //    {
                        //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
                        //    }
                        //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";
                        //    cungDonViTinh = false;
                        //}
                        #endregion

                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        cungDonViTinh = true;
                        currDonViTinh = "";
                        tongKH = 0;
                        tongDC = 0;

                        chiTieuAo = false;
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, 3];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = item.TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH đầu năm
                        cellRange2 = sheet.Cells[beginRow, 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH sau ĐC
                        cellRange2 = sheet.Cells[beginRow, 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                    #endregion
                }
                else
                {
                    #region Chi tieu cap 3
                    beginRow++;
                    if (chiTieuAo)
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        //if (sttLevel2 == 1)
                        //{
                        //    dvPhuTrachCTCap2 = item.DV_PHU_TRACH;
                        //    currDonViTinh = item.DON_VI_TINH;
                        //    cungDonViTinh = true;
                        //    tongKH = tongKH + item.KE_HOACH;
                        //    tongDC = tongDC + item.DIEU_CHINH;
                        //}
                        //else
                        //{
                        //    if (cungDonViTinh)
                        //    {
                        //        if ((currDonViTinh != null) && (item.DON_VI_TINH != null) &&
                        //            (currDonViTinh.Trim().ToUpper() == item.DON_VI_TINH.Trim().ToUpper()))
                        //        {
                        //            cungDonViTinh = true;
                        //            tongKH = tongKH + item.KE_HOACH;
                        //            tongDC = tongDC + item.DIEU_CHINH;
                        //        }
                        //        else
                        //        {
                        //            cungDonViTinh = false;
                        //        }
                        //    }

                        //    if (dvPhuTrachCTCap2 != item.DV_PHU_TRACH) dvPhuTrachCTCap2 = "";
                        //}

                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                    cellRange3.Value = item.TEN_CHI_TIEU;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //Don vi giam sat
                    cellRange3 = sheet.Cells[beginRow, startCol + 3];
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.WrapText = true;
                    cellRange3.Value = item.DV_PHU_TRACH;

                    //Don vi tinh
                    cellRange3 = sheet.Cells[beginRow, startCol + 4];
                    cellRange3.Value = item.DON_VI_TINH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.WrapText = true;

                    //KH đầu năm
                    cellRange3 = sheet.Cells[beginRow, startCol + 5];
                    cellRange3.Value = item.KE_HOACH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    if ((item.KE_HOACH < 1) && (item.KE_HOACH > 0))
                    {
                        cellRange3.Style.Numberformat.Format = "0.##";
                    }
                    else if (item.KE_HOACH % 1 == 0)
                    {
                        cellRange3.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange3.Style.Numberformat.Format = "###,###,###.##";

                    //KH sau ĐC
                    if (item.CHO_PHEP_TINH_TONG == 1)
                    {
                        giaTriDieuChinh = item.KE_HOACH + item.DIEU_CHINH;
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Value = giaTriDieuChinh;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((giaTriDieuChinh < 1) && (giaTriDieuChinh > 0))
                        {
                            cellRange3.Style.Numberformat.Format = "0.##";
                        }
                        else if (giaTriDieuChinh % 1 == 0)
                        {
                            cellRange3.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                    }
                    else
                    {
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    }
                    #endregion
                }
            }
            #region In Tong chi tieu (neu co)
            //if (cungDonViTinh)
            //{
            //    var cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 4];
            //    cellRangeSumUp.Value = dvPhuTrachCTCap2;

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 5];
            //    cellRangeSumUp.Value = currDonViTinh;

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 6];
            //    cellRangeSumUp.Value = tongKH;
            //    if (tongKH % 1 == 0)
            //    {
            //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
            //    }
            //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 7];
            //    cellRangeSumUp.Value = tongDC;
            //    if (tongDC % 1 == 0)
            //    {
            //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
            //    }
            //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";
            //    cungDonViTinh = false;
            //}
            #endregion

            return excelPackage;

        }
        #endregion

        #region Sheet 2
        public FileResult ExporBanHanhDieuChinhDonVi(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            ExcelPackage ep = this.getExcelPackBanHanhDieuChinhDonVi(namKHId, namKH, donviid, tenDonVi);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDonVi.xlsx");
            return result;
        }

        public FileResult PrintBanHanhDieuChinhDonVi(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            ExcelPackage p = getExcelPackBanHanhDieuChinhDonVi(namKHId, namKH, donviid, tenDonVi);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDonVi");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackBanHanhDieuChinhDonVi(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            string donViBaoCao = tenDonVi;
            DonViBO dvBO = new DonViBO();
            DonVi dvObj = dvBO.GetById(donviid);
            if (dvObj != null)
            {
                donViBaoCao = dvObj.TenDonVi;
            }

            KeHoachBO khBO = new KeHoachBO();
            List<BC_DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDonViChuaChinhThuc(namKHId, donviid); //khBO.GetDieuChinhDonVi(namKHId, donviid);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, lstDC.Count + 10, 7];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 7];


            titleCell.Merge = true;
            titleCell.Value = "ĐIỀU CHỈNH CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin don vi bao cao
            startRowTile += 1;
            var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 7];
            donViThucHien.Merge = true;
            donViThucHien.Value = donViBaoCao.ToUpper();
            donViThucHien.Style.Font.Bold = true;
            donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 44;
            sheet.Column(4).Width = 12;
            sheet.Column(5).Width = 12;
            sheet.Column(6).Width = 10;
            sheet.Column(7).Width = 10;

            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;

            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Tên chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;
            sheet.Row(startRow).Height = 40;

            cellRange1 = sheet.Cells[startRow, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 5];
            cellRange1.Merge = true;
            cellRange1.Value = "KH đầu năm";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 6];
            cellRange1.Merge = true;
            cellRange1.Value = "KH sau ĐC";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            int beginRow = startRow;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            int rowIndexCTCap2 = 0;
            foreach (BC_DIEU_CHINH_GIUA_NAM item in lstDC)
            {
                if (item.CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                    //DV giam sat
                    cellRange2 = sheet.Cells[beginRow, 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Đơn vị tính
                    cellRange2 = sheet.Cells[beginRow, 5];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //KH đầu năm
                    cellRange2 = sheet.Cells[beginRow, 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //KH sau ĐC
                    cellRange2 = sheet.Cells[beginRow, 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    #endregion
                }
                else if (item.CAP == 2)
                {
                    if (item.LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        chiTieuAo = false;
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, 3];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = item.TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH đầu năm
                        cellRange2 = sheet.Cells[beginRow, 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH sau ĐC
                        cellRange2 = sheet.Cells[beginRow, 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                }
                else
                {
                    beginRow++;
                    if (chiTieuAo)
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                    cellRange3.Value = item.TEN_CHI_TIEU;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //Don vi giam sat
                    cellRange3 = sheet.Cells[beginRow, startCol + 3];
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.WrapText = true;
                    cellRange3.Value = item.DV_PHU_TRACH;

                    //Don vi tinh
                    cellRange3 = sheet.Cells[beginRow, startCol + 4];
                    cellRange3.Value = item.DON_VI_TINH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.WrapText = true;

                    //KH đầu năm
                    cellRange3 = sheet.Cells[beginRow, startCol + 5];

                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    try
                    {
                        double val = System.Convert.ToDouble(item.KE_HOACH);
                        cellRange3.Value = val;

                        if ((val < 1) && (val > 0))
                        {
                            cellRange3.Style.Numberformat.Format = "0.##";
                        }
                        else if (val % 1 == 0)
                        {
                            cellRange3.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                    }
                    catch
                    {
                        cellRange3.Value = item.KE_HOACH;
                    }

                    //KH sau ĐC
                    cellRange3 = sheet.Cells[beginRow, startCol + 6];
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    try
                    {
                        double val = System.Convert.ToDouble(item.DIEU_CHINH);
                        cellRange3.Value = val;

                        if ((val < 1) && (val > 0))
                        {
                            cellRange3.Style.Numberformat.Format = "0.##";
                        }
                        else if (val % 1 == 0)
                        {
                            cellRange3.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                    }
                    catch
                    {
                        cellRange3.Value = item.DIEU_CHINH;
                    }

                }
            }

            return excelPackage;

        }

        public FileResult ExporBanHanhDieuChinhDonViAllDV(int namKHId, string namKH)
        {
            ExcelPackage ep = this.getExcelPackBanHanhDieuChinhDonViAllDV(namKHId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDonVi.xlsx");
            return result;
        }

        public FileResult PrintBanHanhDieuChinhDonViAllDV(int namKHId, string namKH)
        {
            ExcelPackage p = getExcelPackBanHanhDieuChinhDonViAllDV(namKHId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDonVi");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackBanHanhDieuChinhDonViAllDV(int namKHId, string namKH)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            string donViBaoCao = string.Empty;
            DonViBO dvBO = new DonViBO();
            KeHoachBO khBO = new KeHoachBO();

            List<DonVi> lstDonVi = dvBO.GetDonViByType(false);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";

            foreach (DonVi dv in lstDonVi)
            {
                List<BC_DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDonViChuaChinhThuc(namKHId, dv.DonViId);

                if (lstDC.Count > 0)
                {
                    donViBaoCao = dv.TenDonVi;

                    var sheet = excelPackage.Workbook.Worksheets.Add(dv.TenVietTat);
                    sheet.PrinterSettings.TopMargin = topMargin;
                    sheet.PrinterSettings.BottomMargin = bottomMargin;
                    sheet.PrinterSettings.LeftMargin = leftMargin;
                    sheet.PrinterSettings.RightMargin = rightMargin;

                    var printArea = sheet.Cells[1, 1, lstDC.Count + 10, 7];
                    printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                    // output a line for the headers
                    sheet.Name = dv.TenVietTat;
                    int startRowLogo = 1;
                    var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                    donViCell.Merge = true;
                    donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                    donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                    donViCell.Style.Font.Bold = true;
                    donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    int startRowTile = 3;

                    var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 7];


                    titleCell.Merge = true;
                    titleCell.Value = "ĐIỀU CHỈNH CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
                    titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                    titleCell.Style.Font.Bold = true;
                    titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    #region Thong tin don vi bao cao
                    startRowTile += 1;
                    var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 7];
                    donViThucHien.Merge = true;
                    donViThucHien.Value = donViBaoCao.ToUpper();
                    donViThucHien.Style.Font.Bold = true;
                    donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    #endregion

                    #region Set Column Width
                    sheet.Column(1).Width = 4;
                    sheet.Column(2).Width = 5;
                    sheet.Column(3).Width = 44;
                    sheet.Column(4).Width = 12;
                    sheet.Column(5).Width = 12;
                    sheet.Column(6).Width = 10;
                    sheet.Column(7).Width = 10;

                    #endregion

                    startRow = startRowTile + 1;
                    //STT
                    var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
                    cellRange0.Merge = true;
                    cellRange0.Value = "STT";
                    cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange0.Style.Font.Bold = true;
                    sheet.Column(startCol).Style.WrapText = true;
                    sheet.Column(startCol + 1).Style.WrapText = true;

                    //Chitieu
                    var cellRange1 = sheet.Cells[startRow, startCol + 2];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Tên chỉ tiêu";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    sheet.Column(startCol + 2).Style.WrapText = true;

                    //Don vi giam sat
                    cellRange1 = sheet.Cells[startRow, startCol + 3];
                    cellRange1.Merge = true;
                    cellRange1.Value = "ĐV phân bổ và giám sát";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;
                    sheet.Row(startRow).Height = 40;

                    cellRange1 = sheet.Cells[startRow, startCol + 4];
                    cellRange1.Merge = true;
                    cellRange1.Value = "Đơn vị tính";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheet.Cells[startRow, startCol + 5];
                    cellRange1.Merge = true;
                    cellRange1.Value = "KH đầu năm";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    cellRange1 = sheet.Cells[startRow, startCol + 6];
                    cellRange1.Merge = true;
                    cellRange1.Value = "KH sau ĐC";
                    cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange1.Style.Font.Bold = true;
                    cellRange1.Style.WrapText = true;

                    int beginRow = startRow;
                    int beginCol = startCol;
                    int sttLevel1 = 1;
                    int sttLevel2 = 1;
                    bool chiTieuAo = false;
                    int rowIndexCTCap2 = 0;
                    foreach (BC_DIEU_CHINH_GIUA_NAM item in lstDC)
                    {
                        if (item.CAP == 1)
                        {
                            #region Chi tieu cap 1
                            beginRow++;
                            var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                            //DV giam sat
                            cellRange2 = sheet.Cells[beginRow, 4];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //Đơn vị tính
                            cellRange2 = sheet.Cells[beginRow, 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //KH đầu năm
                            cellRange2 = sheet.Cells[beginRow, 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //KH sau ĐC
                            cellRange2 = sheet.Cells[beginRow, 7];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            #endregion
                        }
                        else if (item.CAP == 2)
                        {
                            if (item.LA_CHI_TIEU_AO == 1)
                            {
                                chiTieuAo = true;
                            }
                            else
                            {

                                beginRow++;

                                rowIndexCTCap2 = beginRow;
                                chiTieuAo = false;
                                //STT
                                var cellRange2 = sheet.Cells[beginRow, 1];
                                cellRange2.Style.Font.Bold = true;
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                cellRange2.Value = sttLevel1.ToString();

                                cellRange2 = sheet.Cells[beginRow, 2];
                                cellRange2.Merge = true;
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                cellRange2.Value = "";

                                //Ten chi tieu
                                cellRange2 = sheet.Cells[beginRow, 3];
                                cellRange2.Style.Font.Bold = true;
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                cellRange2.Value = item.TEN_CHI_TIEU;

                                //Don vi giam sat
                                cellRange2 = sheet.Cells[beginRow, 4];
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                cellRange2.Style.WrapText = true;

                                //Don vi tinh
                                cellRange2 = sheet.Cells[beginRow, 5];
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                                //KH đầu năm
                                cellRange2 = sheet.Cells[beginRow, 6];
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                                //KH sau ĐC
                                cellRange2 = sheet.Cells[beginRow, 7];
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                                cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                                sttLevel1 = sttLevel1 + 1;
                                sttLevel2 = 1;
                            }
                        }
                        else
                        {
                            beginRow++;
                            if (chiTieuAo)
                            {
                                //STT
                                var cellRange2 = sheet.Cells[beginRow, 1];
                                cellRange2.Style.Font.Bold = true;
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                cellRange2.Value = sttLevel1.ToString();

                                cellRange2 = sheet.Cells[beginRow, 2];
                                cellRange2.Merge = true;
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                                sttLevel1 = sttLevel1 + 1;
                                sttLevel2 = 1;

                            }
                            else
                            {
                                //STT
                                var cellRange2 = sheet.Cells[beginRow, 1];
                                cellRange2.Style.Font.Bold = true;
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                                cellRange2 = sheet.Cells[beginRow, 2];
                                cellRange2.Merge = true;
                                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                                cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                                sttLevel2 = sttLevel2 + 1;
                            }

                            //Ten chi tieu
                            var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                            cellRange3.Value = item.TEN_CHI_TIEU;
                            cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            //Don vi giam sat
                            cellRange3 = sheet.Cells[beginRow, startCol + 3];
                            cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange3.Style.WrapText = true;
                            cellRange3.Value = item.DV_PHU_TRACH;

                            //Don vi tinh
                            cellRange3 = sheet.Cells[beginRow, startCol + 4];
                            cellRange3.Value = item.DON_VI_TINH;
                            cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange3.Style.WrapText = true;

                            //KH đầu năm
                            cellRange3 = sheet.Cells[beginRow, startCol + 5];

                            cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            try
                            {
                                double val = System.Convert.ToDouble(item.KE_HOACH);
                                cellRange3.Value = val;

                                if ((val < 1) && (val > 0))
                                {
                                    cellRange3.Style.Numberformat.Format = "0.##";
                                }
                                else if (val % 1 == 0)
                                {
                                    cellRange3.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                            }
                            catch
                            {
                                cellRange3.Value = item.KE_HOACH;
                            }

                            //KH sau ĐC
                            cellRange3 = sheet.Cells[beginRow, startCol + 6];
                            cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            try
                            {
                                double val = System.Convert.ToDouble(item.DIEU_CHINH);
                                cellRange3.Value = val;

                                if ((val < 1) && (val > 0))
                                {
                                    cellRange3.Style.Numberformat.Format = "0.##";
                                }
                                else if (val % 1 == 0)
                                {
                                    cellRange3.Style.Numberformat.Format = "###,###,###";
                                }
                                else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                            }
                            catch
                            {
                                cellRange3.Value = item.DIEU_CHINH;
                            }
                        }
                    }
                }

            }
            return excelPackage;

        }

        #endregion

        #region Sheet 2'
        public FileResult ExporBanHanhDieuChinhDonViAll(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            ExcelPackage ep = this.getExcelPackBanHanhDieuChinhDonViAll(namKHId, namKH, donviid, tenDonVi);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDonVi.xlsx");
            return result;
        }

        public FileResult PrintBanHanhDieuChinhDonViAll(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            ExcelPackage p = getExcelPackBanHanhDieuChinhDonViAll(namKHId, namKH, donviid, tenDonVi);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDonVi");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackBanHanhDieuChinhDonViAll(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            string donViBaoCao = tenDonVi;
            DonViBO dvBO = new DonViBO();
            DonVi dvObj = dvBO.GetById(donviid);
            if (dvObj != null)
            {
                donViBaoCao = dvObj.TenDonVi;
            }

            KeHoachBO khBO = new KeHoachBO();
            List<BC_DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDonViChuaChinhThucAll(namKHId, donviid); //khBO.GetDieuChinhDonVi(namKHId, donviid);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, lstDC.Count + 10, 7];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 7];


            titleCell.Merge = true;
            titleCell.Value = "ĐIỀU CHỈNH CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin don vi bao cao
            startRowTile += 1;
            var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 7];
            donViThucHien.Merge = true;
            donViThucHien.Value = donViBaoCao.ToUpper();
            donViThucHien.Style.Font.Bold = true;
            donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 44;
            sheet.Column(4).Width = 12;
            sheet.Column(5).Width = 12;
            sheet.Column(6).Width = 10;
            sheet.Column(7).Width = 10;

            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;

            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Tên chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;
            sheet.Row(startRow).Height = 40;

            cellRange1 = sheet.Cells[startRow, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 5];
            cellRange1.Merge = true;
            cellRange1.Value = "KH đầu năm";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 6];
            cellRange1.Merge = true;
            cellRange1.Value = "KH sau ĐC";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            int beginRow = startRow;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;

            int rowIndexCTCap2 = 0;
            foreach (BC_DIEU_CHINH_GIUA_NAM item in lstDC)
            {
                if (item.CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                    //DV giam sat
                    cellRange2 = sheet.Cells[beginRow, 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Đơn vị tính
                    cellRange2 = sheet.Cells[beginRow, 5];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //KH đầu năm
                    cellRange2 = sheet.Cells[beginRow, 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //KH sau ĐC
                    cellRange2 = sheet.Cells[beginRow, 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    #endregion
                }
                else if (item.CAP == 2)
                {
                    if (item.LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        chiTieuAo = false;
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, 3];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = item.TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH đầu năm
                        cellRange2 = sheet.Cells[beginRow, 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH sau ĐC
                        cellRange2 = sheet.Cells[beginRow, 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                }
                else
                {
                    beginRow++;
                    if (chiTieuAo)
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                    cellRange3.Value = item.TEN_CHI_TIEU;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //Don vi giam sat
                    cellRange3 = sheet.Cells[beginRow, startCol + 3];
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.WrapText = true;
                    cellRange3.Value = item.DV_PHU_TRACH;

                    //Don vi tinh
                    cellRange3 = sheet.Cells[beginRow, startCol + 4];
                    cellRange3.Value = item.DON_VI_TINH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.WrapText = true;

                    //KH đầu năm
                    cellRange3 = sheet.Cells[beginRow, startCol + 5];

                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    try
                    {
                        double val = System.Convert.ToDouble(item.KE_HOACH);
                        cellRange3.Value = val;

                        if ((val < 1) && (val > 0))
                        {
                            cellRange3.Style.Numberformat.Format = "0.##";
                        }
                        else if (val % 1 == 0)
                        {
                            cellRange3.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                    }
                    catch
                    {
                        cellRange3.Value = item.KE_HOACH;
                    }

                    //KH sau ĐC
                    cellRange3 = sheet.Cells[beginRow, startCol + 6];
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    try
                    {
                        double val = System.Convert.ToDouble(item.DIEU_CHINH);
                        cellRange3.Value = val;

                        if ((val < 1) && (val > 0))
                        {
                            cellRange3.Style.Numberformat.Format = "0.##";
                        }
                        else if (val % 1 == 0)
                        {
                            cellRange3.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                    }
                    catch
                    {
                        cellRange3.Value = item.DIEU_CHINH;
                    }
                }
            }
            return excelPackage;

        }

        #endregion

        #region Sheet 3
        public FileResult ExportDC03(int namKHId, string namKH, int machitieucap1)
        {
            ExcelPackage ep = getExcelPackDC03(namKHId, namKH, machitieucap1);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDonVi.xlsx");
            return result;
        }

        public FileResult PrintDC03(int namKHId, string namKH, int machitieucap1)
        {
            ExcelPackage p = getExcelPackDC03(namKHId, namKH, machitieucap1);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDonVi");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackDC03(int namKHId, string namKH, int machitieucap1)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            //string donViBaoCao = tenDonVi;
            //DonViBO dvBO = new DonViBO();
            //DonVi dvObj = dvBO.GetById(donviid);
            //if (dvObj != null)
            //{
            //    donViBaoCao = dvObj.TenDonVi;
            //}

            string mangBC = "";
            ChiTieuBO ctBO = new ChiTieuBO();
            ChiTieu ctObj = ctBO.GetByMaCap1_Cap(1, machitieucap1.ToString()).FirstOrDefault();
            if (ctObj != null)
            {
                mangBC = ctObj.TEN_CHI_TIEU;
            }

            KeHoachBO khBO = new KeHoachBO();
            List<BC_DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetDieuChinhDC03(namKHId, machitieucap1); //khBO.GetDieuChinhDonVi(namKHId, donviid);

            #region Header
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            Color ctCap3BG = Color.FromArgb(250, 237, 197);
            ExcelPackage excelPackage = new ExcelPackage();
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("ĐỀ NGHỊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, lstDC.Count*2, 10];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "De nghi dieu chinh ke hoach " + namKHId;
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];


            titleCell.Merge = true;
            titleCell.Value = "ĐỀ NGHỊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Thong tin mang chi tieu
            startRowTile += 1;
            var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 10];
            donViThucHien.Merge = true;
            donViThucHien.Value = "Mảng: " + mangBC;
            donViThucHien.Style.Font.Bold = true;
            donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 35;
            sheet.Column(4).Width = 12;
            sheet.Column(5).Width = 12;
            sheet.Column(6).Width = 10;
            sheet.Column(7).Width = 10;
            sheet.Column(8).Width = 10;
            sheet.Column(9).Width = 20;
            sheet.Column(10).Width = 20;
            #endregion

            startRow = startRowTile + 1;

            #region Column header
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;

            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Tên chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            //Don vi giam sat
            cellRange1 = sheet.Cells[startRow, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "ĐV phân bổ và giám sát";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;
            sheet.Row(startRow).Height = 40;

            cellRange1 = sheet.Cells[startRow, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 5];
            cellRange1.Merge = true;
            cellRange1.Value = "KH đầu năm";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 6];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị đề xuất";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 7];
            cellRange1.Merge = true;
            cellRange1.Value = "Ban Duyệt";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 8];
            cellRange1.Merge = true;
            cellRange1.Value = "Cơ sở đề xuất của đơn vị";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 9];
            cellRange1.Merge = true;
            cellRange1.Value = "Cơ sở duyệt của Ban chức năng";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;
            #endregion

            int beginRow = startRow;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            int rowIndexCTCap2 = 0;
            int ChiTieuID = -1;

            int chitieuRowIndex = 0;
            double kehoachDauNam = 0;
            double dieuchinhDelta = 0;
            double pheduyetDelta = 0;

            foreach (BC_DIEU_CHINH_GIUA_NAM item in lstDC)
            {
                if (item.CAP == 1)
                {
                    #region Chi tieu cap 1
                    //beginRow++;
                    //var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    //cellRange2.Merge = true;
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                    ////DV giam sat
                    //cellRange2 = sheet.Cells[beginRow, 4];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    ////Đơn vị tính
                    //cellRange2 = sheet.Cells[beginRow, 5];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    ////KH đầu năm
                    //cellRange2 = sheet.Cells[beginRow, 6];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    ////De xuat
                    //cellRange2 = sheet.Cells[beginRow, 7];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    ////Duyet
                    //cellRange2 = sheet.Cells[beginRow, 8];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    ////Comment DV
                    //cellRange2 = sheet.Cells[beginRow, 9];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    ////Comment ban
                    //cellRange2 = sheet.Cells[beginRow, 10];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    #endregion
                }
                else if (item.CAP == 2)
                {
                    #region Chi tieu cap 2
                    if (item.LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        chiTieuAo = false;
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, 3];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = item.TEN_CHI_TIEU;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //KH đầu năm
                        cellRange2 = sheet.Cells[beginRow, 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //De xuat
                        cellRange2 = sheet.Cells[beginRow, 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Duyet
                        cellRange2 = sheet.Cells[beginRow, 8];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Comment DV
                        cellRange2 = sheet.Cells[beginRow, 9];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Comment ban
                        cellRange2 = sheet.Cells[beginRow, 10];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                    #endregion
                }
                else
                {
                    beginRow++;
                    if (ChiTieuID != item.CHI_TIEU_ID)
                    {
                        #region In so lieu dieu chinh
                        if (chitieuRowIndex > 0)
                        {
                            sheet.Cells[chitieuRowIndex, 7].Value = dieuchinhDelta;
                            sheet.Cells[chitieuRowIndex, 8].Value = pheduyetDelta;
                            chitieuRowIndex = 0;
                        }
                        #endregion

                        chitieuRowIndex = beginRow;
                        kehoachDauNam = khBO.GetKhDHQGByChiTieu(namKHId, item.CHI_TIEU_ID);
                        dieuchinhDelta = kehoachDauNam + (String2Double(item.DIEU_CHINH) - String2Double(item.KE_HOACH));
                        pheduyetDelta = kehoachDauNam + (String2Double(item.GIA_TRI_DUYET) - String2Double(item.KE_HOACH));
                        ChiTieuID = item.CHI_TIEU_ID;
                        if (chiTieuAo)
                        {
                            //STT
                            var cellRange2 = sheet.Cells[beginRow, 1];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                            cellRange2 = sheet.Cells[beginRow, 2];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(ctCap3BG);
                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;

                        }
                        else
                        {
                            //STT
                            var cellRange2 = sheet.Cells[beginRow, 1];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                            cellRange2 = sheet.Cells[beginRow, 2];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                            sttLevel2 = sttLevel2 + 1;
                        }

                        //Ten chi tieu
                        var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                        cellRange3.Value = item.TEN_CHI_TIEU;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);
                        cellRange3.Style.WrapText = true;

                        //Don vi giam sat
                        cellRange3 = sheet.Cells[beginRow, startCol + 3];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                        cellRange3.Value = item.DV_PHU_TRACH;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        //Don vi tinh
                        cellRange3 = sheet.Cells[beginRow, startCol + 4];
                        cellRange3.Value = item.DON_VI_TINH;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        //Ke hoach dau nam
                        cellRange3 = sheet.Cells[beginRow, startCol + 5];
                        cellRange3.Value = kehoachDauNam; //"";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        //
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        cellRange3 = sheet.Cells[beginRow, startCol + 7];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        cellRange3 = sheet.Cells[beginRow, startCol + 8];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        cellRange3 = sheet.Cells[beginRow, startCol + 9];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange3.Style.Fill.BackgroundColor.SetColor(ctCap3BG);

                        beginRow = beginRow + 1;

                        cellRange3 = sheet.Cells[beginRow, startCol];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange3 = sheet.Cells[beginRow, startCol + 1];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Ten don vi
                        cellRange3 = sheet.Cells[beginRow, startCol + 2];
                        cellRange3.Value = item.DON_VI;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;

                        //Don vi giam sat
                        cellRange3 = sheet.Cells[beginRow, startCol + 3];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                        cellRange3.Value = item.DV_PHU_TRACH;

                        //Don vi tinh
                        cellRange3 = sheet.Cells[beginRow, startCol + 4];
                        cellRange3.Value = item.DON_VI_TINH;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                        //KH đầu năm
                        cellRange3 = sheet.Cells[beginRow, startCol + 5];

                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        try
                        {
                            double val = System.Convert.ToDouble(item.KE_HOACH);
                            cellRange3.Value = val;

                            if ((val < 1) && (val > 0))
                            {
                                cellRange3.Style.Numberformat.Format = "0.##";
                            }
                            else if (val % 1 == 0)
                            {
                                cellRange3.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                        }
                        catch
                        {
                            cellRange3.Value = item.KE_HOACH;
                        }

                        //De xuat
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        try
                        {
                            double val = System.Convert.ToDouble(item.DIEU_CHINH);
                            cellRange3.Value = val;

                            if ((val < 1) && (val > 0))
                            {
                                cellRange3.Style.Numberformat.Format = "0.##";
                            }
                            else if (val % 1 == 0)
                            {
                                cellRange3.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                        }
                        catch
                        {
                            cellRange3.Value = item.DIEU_CHINH;
                        }

                        //Duyet
                        cellRange3 = sheet.Cells[beginRow, startCol + 7];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        try
                        {
                            double val = System.Convert.ToDouble(item.GIA_TRI_DUYET);
                            cellRange3.Value = val;

                            if ((val < 1) && (val > 0))
                            {
                                cellRange3.Style.Numberformat.Format = "0.##";
                            }
                            else if (val % 1 == 0)
                            {
                                cellRange3.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                        }
                        catch
                        {
                            cellRange3.Value = item.GIA_TRI_DUYET;
                        }

                        //Comment DV
                        cellRange3 = sheet.Cells[beginRow, startCol + 8];
                        cellRange3.Value = item.COMMENT_DV;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;

                        //Comment DV
                        cellRange3 = sheet.Cells[beginRow, startCol + 9];
                        cellRange3.Value = item.COMMENT_BAN;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                    }
                    else
                    {
                        dieuchinhDelta = dieuchinhDelta + (String2Double(item.DIEU_CHINH) - String2Double(item.KE_HOACH));
                        pheduyetDelta = pheduyetDelta + (String2Double(item.GIA_TRI_DUYET) - String2Double(item.KE_HOACH));

                        var cellRange3 = sheet.Cells[beginRow, startCol];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange3 = sheet.Cells[beginRow, startCol + 1];
                        cellRange3.Value = "";
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Ten don vi
                        cellRange3 = sheet.Cells[beginRow, startCol + 2];
                        cellRange3.Value = item.DON_VI;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Don vi giam sat
                        cellRange3 = sheet.Cells[beginRow, startCol + 3];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                        cellRange3.Value = item.DV_PHU_TRACH;

                        //Don vi tinh
                        cellRange3 = sheet.Cells[beginRow, startCol + 4];
                        cellRange3.Value = item.DON_VI_TINH;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                        //KH đầu năm
                        cellRange3 = sheet.Cells[beginRow, startCol + 5];

                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        try
                        {
                            double val = System.Convert.ToDouble(item.KE_HOACH);
                            cellRange3.Value = val;

                            if ((val < 1) && (val > 0))
                            {
                                cellRange3.Style.Numberformat.Format = "0.##";
                            }
                            else if (val % 1 == 0)
                            {
                                cellRange3.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                        }
                        catch
                        {
                            cellRange3.Value = item.KE_HOACH;
                        }

                        //De xuat
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        try
                        {
                            double val = System.Convert.ToDouble(item.DIEU_CHINH);
                            cellRange3.Value = val;

                            if ((val < 1) && (val > 0))
                            {
                                cellRange3.Style.Numberformat.Format = "0.##";
                            }
                            else if (val % 1 == 0)
                            {
                                cellRange3.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                        }
                        catch
                        {
                            cellRange3.Value = item.DIEU_CHINH;
                        }

                        //Duyet
                        cellRange3 = sheet.Cells[beginRow, startCol + 7];
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        try
                        {
                            double val = System.Convert.ToDouble(item.GIA_TRI_DUYET);
                            cellRange3.Value = val;

                            if ((val < 1) && (val > 0))
                            {
                                cellRange3.Style.Numberformat.Format = "0.##";
                            }
                            else if (val % 1 == 0)
                            {
                                cellRange3.Style.Numberformat.Format = "###,###,###";
                            }
                            else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                        }
                        catch
                        {
                            cellRange3.Value = item.GIA_TRI_DUYET;
                        }

                        //Comment DV
                        cellRange3 = sheet.Cells[beginRow, startCol + 8];
                        cellRange3.Value = item.COMMENT_DV;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;

                        //Comment DV
                        cellRange3 = sheet.Cells[beginRow, startCol + 9];
                        cellRange3.Value = item.COMMENT_DV;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange3.Style.WrapText = true;
                    }
                }
                #region In so lieu dieu chinh
                if (chitieuRowIndex > 0)
                {
                    sheet.Cells[chitieuRowIndex, 7].Value = dieuchinhDelta;
                    sheet.Cells[chitieuRowIndex, 8].Value = pheduyetDelta;
                    chitieuRowIndex = 0;
                }
                #endregion
            }

            return excelPackage;

        }

        #endregion

        #region Sheet 4
        public FileResult ExportTongHopDieuChinhTheoDonVi(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            ExcelPackage ep = this.getExcelPackTongHopDieuChinhTheoDonVi(namKHId, namKH, donviid, tenDonVi);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhChiTieuKeHoachDonVi.xlsx");
            return result;
        }

        public FileResult PrintTongHopDieuChinhTheoDonVi(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            ExcelPackage p = getExcelPackTongHopDieuChinhTheoDonVi(namKHId, namKH, donviid, tenDonVi);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;

            Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "TongHopDieuChinhTheoDonVi");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackTongHopDieuChinhTheoDonVi(int namKHId, string namKH, int donviid, string tenDonVi)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();

            string donViBaoCao = tenDonVi;
            DonViBO dvBO = new DonViBO();
            DonVi dvObj = dvBO.GetById(donviid);
            if (dvObj != null)
            {
                donViBaoCao = dvObj.TenDonVi;
            }


            KeHoachBO khBO = new KeHoachBO();
            List<DIEU_CHINH_GIUA_NAM> lstDC = khBO.GetTongHopDieuChinhDonVi(namKHId, donviid);

            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();
            //using (var excelPackage = new ExcelPackage())
            //{
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tong hop ĐC theo đơn vị " + namKH);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, 1000, 8];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tổng hợp ĐC theo đơn vị";
            int startRowLogo = 1;
            var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            donViCell.Merge = true;
            donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            donViCell.Style.Font.Bold = true;
            donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTile = 3;

            var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];


            titleCell.Merge = true;
            titleCell.Value = "BÁO CÁO ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ KỲ SƠ KẾT NĂM " + namKH;
            titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            titleCell.Style.Font.Bold = true;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

            #region Thong tin don vi bao cao
            startRowTile += 1;
            var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 8];
            donViThucHien.Merge = true;
            donViThucHien.Value = "Đơn vị: " + donViBaoCao.ToUpper();
            donViThucHien.Style.Font.Bold = true;
            donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            #endregion

            #region Set Column Width
            sheet.Column(1).Width = 4;
            sheet.Column(2).Width = 5;
            sheet.Column(3).Width = 44;
            sheet.Column(4).Width = 12;
            sheet.Column(5).Width = 12;
            sheet.Column(6).Width = 12;
            sheet.Column(7).Width = 12;
            sheet.Column(8).Width = 40;

            #endregion

            startRow = startRowTile + 1;
            //STT
            var cellRange0 = sheet.Cells[startRow, startCol, startRow, startCol + 1];
            cellRange0.Merge = true;
            cellRange0.Value = "STT";
            cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange0.Style.Font.Bold = true;
            sheet.Column(startCol).Style.WrapText = true;
            sheet.Column(startCol + 1).Style.WrapText = true;

            //Chitieu
            var cellRange1 = sheet.Cells[startRow, startCol + 2];
            cellRange1.Merge = true;
            cellRange1.Value = "Tên chỉ tiêu";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            sheet.Column(startCol + 2).Style.WrapText = true;

            ////Don vi giam sat
            //cellRange1 = sheet.Cells[startRow, startCol + 3];
            //cellRange1.Merge = true;
            //cellRange1.Value = "ĐV phân bổ và giám sát";
            //cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            //cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            //cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            //cellRange1.Style.Font.Bold = true;
            //cellRange1.Style.WrapText = true;
            //sheet.Row(startRow).Height = 40;

            cellRange1 = sheet.Cells[startRow, startCol + 3];
            cellRange1.Merge = true;
            cellRange1.Value = "Đơn vị tính";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 4];
            cellRange1.Merge = true;
            cellRange1.Value = "Kế hoạch đầu năm";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 5];
            cellRange1.Merge = true;
            cellRange1.Value = "Kết quả hoàn thành kỳ sơ kết";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 6];
            cellRange1.Merge = true;
            cellRange1.Value = "Đề xuất điều chỉnh";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            cellRange1 = sheet.Cells[startRow, startCol + 7];
            cellRange1.Merge = true;
            cellRange1.Value = "Căn cứ đề xuất điều chỉnh";
            cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
            cellRange1.Style.Font.Bold = true;
            cellRange1.Style.WrapText = true;

            sheet.Row(startRow).Height = 84;

            int beginRow = startRow;
            int beginCol = startCol;
            int sttLevel1 = 1;
            int sttLevel2 = 1;
            bool chiTieuAo = false;
            bool cungDonViTinh = false;
            string currDonViTinh = "";
            double tongKH = 0;
            double tongDC = 0;
            //double tyLeDC = 0;
            string dvPhuTrachCTCap2 = "";
            int rowIndexCTCap2 = 0;
            double dexuat = 0;


            foreach (DIEU_CHINH_GIUA_NAM item in lstDC)
            {
                if (item.CAP == 1)
                {
                    #region Chi tieu cap 1
                    beginRow++;
                    var cellRange2 = sheet.Cells[beginRow, 1, beginRow, 3];
                    cellRange2.Merge = true;
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    cellRange2.Value = item.TEN_CHI_TIEU.ToUpper();

                    ////DV giam sat
                    //cellRange2 = sheet.Cells[beginRow, 4];
                    //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Đơn vị tính
                    cellRange2 = sheet.Cells[beginRow, 4];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Mức chỉ tiêu giao cho đơn vị
                    cellRange2 = sheet.Cells[beginRow, 5];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    //Kết quả hoàn thành kỳ sơ kết  
                    cellRange2 = sheet.Cells[beginRow, 6];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Mức chỉ tiêu đơn vị đề xuất điều chỉnh
                    cellRange2 = sheet.Cells[beginRow, 7];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    //Căn cứ đề xuất điều chỉnh  
                    cellRange2 = sheet.Cells[beginRow, 8];
                    cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                    #endregion
                }
                else if (item.CAP == 2)
                {
                    if (item.LA_CHI_TIEU_AO == 1)
                    {
                        chiTieuAo = true;
                    }
                    else
                    {
                        #region In Tong chi tieu (neu co)
                        //if (cungDonViTinh)
                        //{
                        //    var cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 4];
                        //    cellRangeSumUp.Value = dvPhuTrachCTCap2;

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 5];
                        //    cellRangeSumUp.Value = currDonViTinh;

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 6];
                        //    cellRangeSumUp.Value = tongKH;
                        //    if (tongKH % 1 == 0)
                        //    {
                        //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
                        //    }
                        //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";

                        //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 7];
                        //    cellRangeSumUp.Value = tongDC;
                        //    if (tongDC % 1 == 0)
                        //    {
                        //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
                        //    }
                        //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";
                        //    cungDonViTinh = false;
                        //}
                        #endregion

                        beginRow++;

                        rowIndexCTCap2 = beginRow;
                        cungDonViTinh = true;
                        currDonViTinh = "";
                        tongKH = 0;
                        tongDC = 0;

                        chiTieuAo = false;
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = "";

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, 3];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = item.TEN_CHI_TIEU;

                        ////Don vi giam sat
                        //cellRange2 = sheet.Cells[beginRow, 4];
                        //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        //cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        //cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        //cellRange2.Style.WrapText = true;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, 4];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Mức chỉ tiêu giao cho đơn vị
                        cellRange2 = sheet.Cells[beginRow, 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Kết quả hoàn thành kỳ sơ kết
                        cellRange2 = sheet.Cells[beginRow, 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Mức chỉ tiêu đơn vị đề xuất điều chỉnh
                        cellRange2 = sheet.Cells[beginRow, 7];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Căn cứ đề xuất điều chỉnh
                        cellRange2 = sheet.Cells[beginRow, 8];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;
                    }
                }
                else
                {
                    beginRow++;
                    if (chiTieuAo)
                    {
                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Value = sttLevel1.ToString();

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sttLevel1 = sttLevel1 + 1;
                        sttLevel2 = 1;

                    }
                    else
                    {
                        if (sttLevel2 == 1)
                        {
                            dvPhuTrachCTCap2 = item.DV_PHU_TRACH;
                            currDonViTinh = item.DON_VI_TINH;
                            cungDonViTinh = true;
                            tongKH = tongKH + item.KE_HOACH;
                            tongDC = tongDC + item.DIEU_CHINH;
                        }
                        else
                        {
                            if (cungDonViTinh)
                            {
                                if ((currDonViTinh != null) && (item.DON_VI_TINH != null) &&
                                    (currDonViTinh.Trim().ToUpper() == item.DON_VI_TINH.Trim().ToUpper()))
                                {
                                    cungDonViTinh = true;
                                    tongKH = tongKH + item.KE_HOACH;
                                    tongDC = tongDC + item.DIEU_CHINH;
                                }
                                else
                                {
                                    cungDonViTinh = false;
                                }
                            }

                            if (dvPhuTrachCTCap2 != item.DV_PHU_TRACH) dvPhuTrachCTCap2 = "";
                        }

                        //STT
                        var cellRange2 = sheet.Cells[beginRow, 1];
                        cellRange2.Style.Font.Bold = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        cellRange2 = sheet.Cells[beginRow, 2];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        sttLevel2 = sttLevel2 + 1;
                    }

                    //Ten chi tieu
                    var cellRange3 = sheet.Cells[beginRow, startCol + 2];
                    cellRange3.Value = item.TEN_CHI_TIEU;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ////Don vi giam sat
                    //cellRange3 = sheet.Cells[beginRow, startCol + 3];
                    //cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    //cellRange3.Style.WrapText = true;
                    //cellRange3.Value = item.DV_PHU_TRACH;

                    //Don vi tinh
                    cellRange3 = sheet.Cells[beginRow, startCol + 3];
                    cellRange3.Value = item.DON_VI_TINH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cellRange3.Style.WrapText = true;

                    //Mức chỉ tiêu giao cho đơn vị
                    cellRange3 = sheet.Cells[beginRow, startCol + 4];
                    cellRange3.Value = item.KE_HOACH;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    if ((item.KE_HOACH < 1) && (item.KE_HOACH > 0))
                    {
                        cellRange3.Style.Numberformat.Format = "0.##";
                    }
                    else if (item.KE_HOACH % 1 == 0)
                    {
                        cellRange3.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange3.Style.Numberformat.Format = "###,###,###.##";

                    //Kết quả hoàn thành kỳ sơ kết 
                    cellRange3 = sheet.Cells[beginRow, startCol + 5];
                    cellRange3.Value = item.THUC_HIEN;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    if ((item.THUC_HIEN < 1) && (item.THUC_HIEN > 0))
                    {
                        cellRange3.Style.Numberformat.Format = "0.##";
                    }
                    else if (item.THUC_HIEN % 1 == 0)
                    {
                        cellRange3.Style.Numberformat.Format = "###,###,###";
                    }
                    else cellRange3.Style.Numberformat.Format = "###,###,###.##";

                    //Mức chỉ tiêu đơn vị đề xuất điều chỉnh
                    try
                    {
                        dexuat = System.Convert.ToDouble(item.DE_XUAT);
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Value = dexuat;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((dexuat < 1) && (dexuat > 0))
                        {
                            cellRange3.Style.Numberformat.Format = "0.##";
                        }
                        else if (dexuat % 1 == 0)
                        {
                            cellRange3.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange3.Style.Numberformat.Format = "###,###,###.##";
                    }
                    catch
                    {
                        cellRange3 = sheet.Cells[beginRow, startCol + 6];
                        cellRange3.Value = item.DE_XUAT;
                        cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    }

                    //Căn cứ đề xuất điều chỉnh
                    cellRange3 = sheet.Cells[beginRow, startCol + 7];
                    cellRange3.Value = item.COMMENT_DV;
                    cellRange3.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cellRange3.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    cellRange3.Style.WrapText = true;
                }
            }
            //#region In Tong chi tieu (neu co)
            //if (cungDonViTinh)
            //{
            //    var cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 4];
            //    cellRangeSumUp.Value = dvPhuTrachCTCap2;

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 5];
            //    cellRangeSumUp.Value = currDonViTinh;

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 6];
            //    cellRangeSumUp.Value = tongKH;
            //    if (tongKH % 1 == 0)
            //    {
            //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
            //    }
            //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";

            //    cellRangeSumUp = sheet.Cells[rowIndexCTCap2, 7];
            //    cellRangeSumUp.Value = tongDC;
            //    if (tongDC % 1 == 0)
            //    {
            //        cellRangeSumUp.Style.Numberformat.Format = "###,###,###";
            //    }
            //    else cellRangeSumUp.Style.Numberformat.Format = "###,###,###.##";
            //    cungDonViTinh = false;
            //}
            //#endregion

            return excelPackage;

        }
        #endregion

        #region Sheet 5
        public FileResult ExporDieuChinhTheoMang(int namKHId, string namKH)
        {
            ExcelPackage ep = getExcelPackDieuChinhTheoMangChuaCT(namKHId, namKH); //getExcelPackDieuChinhTheoMang(namKHId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhTheoMang.xlsx");
            return result;
        }

        public FileResult PrintDieuChinhTheoMang(int namKHId, string namKH)
        {
            ExcelPackage p = getExcelPackDieuChinhTheoMangChuaCT(namKHId, namKH); //getExcelPackDieuChinhTheoMang(namKHId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhTheoMang");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        private ExcelPackage getExcelPackDieuChinhTheoMang_Old(int namKHId, string namKH)
        {
            string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_MANG.xlsx");
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = p.Workbook.Worksheets[1];

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                var cellRange = ws.Cells[4, 1, 4, 5];
                cellRange.Merge = true;
                cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange.Value = "THỐNG KÊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;


                var lsttenchitieu = chitieubo.GETCAP1(namKHId);
                var lstkh = kehoachbo.GetSumKH(namKHId, 31);
                var lstdc = kehoachbo.GetSumKH(namKHId, 61);

                int stt = 1;
                startRow = 10;
                string tenct = "";
                double tongCT = 0;
                int giam = 0;
                int tang = 0;


                foreach (var item in lsttenchitieu)
                {
                    tenct = item.TEN_CHI_TIEU;
                    for (int i = 0; i < lstkh.Count; i++)
                    {
                        if (lstkh[i].MA_CAP1 == item.MA_CAP1)
                        {
                            tongCT = tongCT + 1;
                            if (lstkh[i].GIA_TRI > lstdc[i].GIA_TRI) giam = giam + 1;
                            if (lstkh[i].GIA_TRI < lstdc[i].GIA_TRI) tang = tang + 1;
                        }
                    }

                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 2].Value = item.TEN_CHI_TIEU;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 3].Value = tongCT;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 4].Value = giam;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 5].Value = tang;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    tongCT = 0;
                    giam = 0;
                    tang = 0;

                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return p;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private ExcelPackage getExcelPackDieuChinhTheoMang(int namKHId, string namKH)
        {
            //string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_MANG.xlsx");
            //System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                Color backGroundColor = Color.FromArgb(208, 206, 206);
                ExcelPackage excelPackage = new ExcelPackage();

                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                var sheet = excelPackage.Workbook.Worksheets.Add("Tong hop ĐC theo đơn vị " + namKH);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, 1000, 8];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = "Tong hop ĐC theo đơn vị";
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 5];


                titleCell.Merge = true;
                titleCell.Value = "THỐNG KÊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Loai bao cao
                startRowTile += 1;
                var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 5];
                donViThucHien.Merge = true;
                donViThucHien.Value = "Theo mảng hoạt động";
                donViThucHien.Style.Font.Bold = true;
                donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 6;
                sheet.Column(2).Width = 50;
                sheet.Column(3).Width = 12;
                sheet.Column(4).Width = 12;
                sheet.Column(5).Width = 12;


                #endregion

                startRow = startRowTile + 2;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol];
                cellRange0.Merge = true;
                cellRange0.Value = "STT";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;

                //Mảng hoạt động
                var cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Mảng hoạt động";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu kế hoạch đầu năm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu được điều chỉnh";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow + 1, startCol + 3];
                cellRange1.Value = "Điều chỉnh giảm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow + 1, startCol + 4];
                cellRange1.Value = "Điều chỉnh tăng";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                sheet.Row(startRow).Height = 32;
                sheet.Row(startRow + 1).Height = 32;


                //ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = sheet;

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                var lsttenchitieu = chitieubo.GETCAP1(namKHId);
                var lstkh = kehoachbo.GetSumKH(namKHId, 31);
                var lstdc = kehoachbo.GetSumKH(namKHId, 61);

                int stt = 1;
                startRow = startRow + 2;
                string tenct = "";
                double tongCT = 0;
                int giam = 0;
                int tang = 0;


                foreach (var item in lsttenchitieu)
                {
                    tenct = item.TEN_CHI_TIEU;
                    for (int i = 0; i < lstkh.Count; i++)
                    {
                        if (lstkh[i].MA_CAP1 == item.MA_CAP1)
                        {
                            tongCT = tongCT + 1;

                            if (i < lstdc.Count)
                            {
                                if (lstkh[i].GIA_TRI > lstdc[i].GIA_TRI) giam = giam + 1;
                                if (lstkh[i].GIA_TRI < lstdc[i].GIA_TRI) tang = tang + 1;
                            }
                        }
                    }

                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    ws.Cells[startRow, 2].Value = item.TEN_CHI_TIEU;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 3].Value = tongCT;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 4].Value = giam;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 5].Value = tang;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    tongCT = 0;
                    giam = 0;
                    tang = 0;

                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return excelPackage;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private ExcelPackage getExcelPackDieuChinhTheoMangChuaCT(int namKHId, string namKH)
        {
            //string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_MANG.xlsx");
            //System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                Color backGroundColor = Color.FromArgb(208, 206, 206);
                ExcelPackage excelPackage = new ExcelPackage();

                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                var sheet = excelPackage.Workbook.Worksheets.Add("Tong hop ĐC theo đơn vị " + namKH);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, 1000, 8];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = "Tong hop ĐC theo đơn vị";
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 5];


                titleCell.Merge = true;
                titleCell.Value = "THỐNG KÊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Loai bao cao
                startRowTile += 1;
                var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 5];
                donViThucHien.Merge = true;
                donViThucHien.Value = "Theo mảng hoạt động";
                donViThucHien.Style.Font.Bold = true;
                donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 6;
                sheet.Column(2).Width = 50;
                sheet.Column(3).Width = 12;
                sheet.Column(4).Width = 12;
                sheet.Column(5).Width = 12;


                #endregion

                startRow = startRowTile + 2;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol];
                cellRange0.Merge = true;
                cellRange0.Value = "STT";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;

                //Mảng hoạt động
                var cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Mảng hoạt động";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu kế hoạch đầu năm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu được điều chỉnh";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow + 1, startCol + 3];
                cellRange1.Value = "Điều chỉnh giảm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow + 1, startCol + 4];
                cellRange1.Value = "Điều chỉnh tăng";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                sheet.Row(startRow).Height = 32;
                sheet.Row(startRow + 1).Height = 32;


                //ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = sheet;

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                var lsttenchitieu = chitieubo.GETCAP1(namKHId);
                var lstkh = kehoachbo.GetSumKH(namKHId, 31);
                List<SumKH> lstdc = kehoachbo.GetDieuChinhTheoMang(namKHId); //kehoachbo.GetSumKH(namKHId, 61);

                int stt = 1;
                startRow = startRow + 2;
                string tenct = "";
                double tongCT = 0;
                int giam = 0;
                int tang = 0;


                foreach (var item in lsttenchitieu)
                {
                    tenct = item.TEN_CHI_TIEU;
                    for (int i = 0; i < lstkh.Count; i++)
                    {
                        if (lstkh[i].MA_CAP1 == item.MA_CAP1)
                        {
                            tongCT = tongCT + 1;

                            //if (i < lstdc.Count)
                            //{
                            //    if (lstkh[i].GIA_TRI > lstdc[i].GIA_TRI) giam = giam + 1;
                            //    if (lstkh[i].GIA_TRI < lstdc[i].GIA_TRI) tang = tang + 1;
                            //}
                        }
                    }
                    giam = 0;
                    tang = 0;
                    foreach (SumKH idc in lstdc)
                    {
                        if (idc.MA_CAP1.Equals(item.MA_CAP1))
                        {
                            if (idc.GIA_TRI > 0)
                            {
                                tang = tang + 1;
                            }
                            else if (idc.GIA_TRI < 0)
                            {
                                giam = giam + 1;
                            }
                        }
                    }

                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                    ws.Cells[startRow, 2].Value = item.TEN_CHI_TIEU;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 3].Value = tongCT;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 4].Value = giam;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 5].Value = tang;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    tongCT = 0;
                    giam = 0;
                    tang = 0;

                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return excelPackage;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Sheet 6
        public FileResult ExporDieuChinhTheoDV(int namKHId, string namKH)
        {
            ExcelPackage ep = getExcelPackDieuChinhTheoDVChuaCT(namKHId, namKH); //getExcelPackDieuChinhTheoDV(namKHId, namKH);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "DieuChinhTheoMang.xlsx");
            return result;
        }

        public FileResult PrintDieuChinhTheoDV(int namKHId, string namKH)
        {
            ExcelPackage p = getExcelPackDieuChinhTheoDVChuaCT(namKHId, namKH); //getExcelPackDieuChinhTheoDV(namKHId, namKH);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhTheoMang");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }


        private ExcelPackage getExcelPackDieuChinhTheoDV_Old(int namKHId, string namKH)
        {
            string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_DV.xlsx");
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = p.Workbook.Worksheets[1];

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                var cellRange = ws.Cells[4, 1, 4, 5];
                cellRange.Merge = true;
                cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange.Value = "THỐNG KÊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;

                KeHoachBO khBO = new KeHoachBO();
                var lstdv = khBO.GetListDonViDieuChinh(namKHId);


                int stt = 1;
                startRow = 10;
                string tendv = "";
                double tongCT = 0;
                int giam = 0;
                int tang = 0;


                foreach (var item in lstdv)
                {
                    var lstkh = kehoachbo.GetbyDV(namKHId, 31, item.DON_VI_ID);
                    var lstdc = kehoachbo.GetbyDV(namKHId, 61, item.DON_VI_ID);

                    tendv = item.DON_VI;
                    for (int i = 0; i < lstkh.Count; i++)
                    {
                        if (lstkh[i].DON_VI_ID == item.DON_VI_ID)
                        {
                            tongCT = tongCT + 1;
                            if (!string.IsNullOrEmpty(lstkh[i].GIA_TRI) && !string.IsNullOrEmpty(lstdc[i].GIA_TRI) &&
                                (System.Convert.ToDouble(lstkh[i].GIA_TRI) > System.Convert.ToDouble(lstdc[i].GIA_TRI))) giam = giam + 1;

                            if (!string.IsNullOrEmpty(lstkh[i].GIA_TRI) && !string.IsNullOrEmpty(lstdc[i].GIA_TRI) &&
                                (System.Convert.ToDouble(lstkh[i].GIA_TRI) < System.Convert.ToDouble(lstdc[i].GIA_TRI))) tang = tang + 1;
                        }
                    }

                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 2].Value = item.DON_VI;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 3].Value = tongCT;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 4].Value = giam;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 5].Value = tang;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    tongCT = 0;
                    giam = 0;
                    tang = 0;

                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return p;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private ExcelPackage getExcelPackDieuChinhTheoDV(int namKHId, string namKH)
        {
            //string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_DV.xlsx");
            //System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                Color backGroundColor = Color.FromArgb(208, 206, 206);
                ExcelPackage excelPackage = new ExcelPackage();

                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                var sheet = excelPackage.Workbook.Worksheets.Add("Tong hop ĐC theo đơn vị " + namKH);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, 1000, 8];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = "Tong hop ĐC theo đơn vị";
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 5];


                titleCell.Merge = true;
                titleCell.Value = "THỐNG KÊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Loai bao cao
                startRowTile += 1;
                var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 5];
                donViThucHien.Merge = true;
                donViThucHien.Value = "Theo đơn vị";
                donViThucHien.Style.Font.Bold = true;
                donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 6;
                sheet.Column(2).Width = 50;
                sheet.Column(3).Width = 12;
                sheet.Column(4).Width = 12;
                sheet.Column(5).Width = 12;
                #endregion

                startRow = startRowTile + 2;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol];
                cellRange0.Merge = true;
                cellRange0.Value = "STT";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;

                //Tên đơn vị
                var cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Tên đơn vị";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu kế hoạch đầu năm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu được điều chỉnh";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow + 1, startCol + 3];
                cellRange1.Value = "Điều chỉnh giảm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow + 1, startCol + 4];
                cellRange1.Value = "Điều chỉnh tăng";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                sheet.Row(startRow).Height = 32;
                sheet.Row(startRow + 1).Height = 32;


                //ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = sheet;

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                KeHoachBO khBO = new KeHoachBO();
                var lstdv = khBO.GetListDonViDieuChinh(namKHId);

                int stt = 1;
                startRow = startRow + 2;
                string tendv = "";
                double tongCT = 0;
                int giam = 0;
                int tang = 0;


                foreach (var item in lstdv)
                {
                    var lstkh = kehoachbo.GetbyDV(namKHId, 31, item.DON_VI_ID);
                    var lstdc = kehoachbo.GetbyDV(namKHId, 61, item.DON_VI_ID);

                    tendv = item.DON_VI;
                    for (int i = 0; i < lstkh.Count; i++)
                    {
                        if (lstkh[i].DON_VI_ID == item.DON_VI_ID)
                        {
                            tongCT = tongCT + 1;
                            if (i < lstdc.Count)
                            {
                                if (!string.IsNullOrEmpty(lstkh[i].GIA_TRI) && !string.IsNullOrEmpty(lstdc[i].GIA_TRI) &&
                                    (System.Convert.ToDouble(lstkh[i].GIA_TRI) > System.Convert.ToDouble(lstdc[i].GIA_TRI))) giam = giam + 1;

                                if (!string.IsNullOrEmpty(lstkh[i].GIA_TRI) && !string.IsNullOrEmpty(lstdc[i].GIA_TRI) &&
                                    (System.Convert.ToDouble(lstkh[i].GIA_TRI) < System.Convert.ToDouble(lstdc[i].GIA_TRI))) tang = tang + 1;
                            }
                        }
                    }

                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 2].Value = item.DON_VI;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 3].Value = tongCT;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 4].Value = giam;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 5].Value = tang;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    tongCT = 0;
                    giam = 0;
                    tang = 0;

                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return excelPackage;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private ExcelPackage getExcelPackDieuChinhTheoDVChuaCT(int namKHId, string namKH)
        {
            //string filepath = Server.MapPath("~/TemplateExcel/BM_DIEUCHINH_DV.xlsx");
            //System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
            try
            {
                Color backGroundColor = Color.FromArgb(208, 206, 206);
                ExcelPackage excelPackage = new ExcelPackage();

                excelPackage.Workbook.Properties.Author = "vanduong";
                excelPackage.Workbook.Properties.Title = "Export excel";
                var sheet = excelPackage.Workbook.Worksheets.Add("Tong hop ĐC theo đơn vị " + namKH);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, 1000, 8];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = "Tong hop ĐC theo đơn vị";
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 5];


                titleCell.Merge = true;
                titleCell.Value = "THỐNG KÊ ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Loai bao cao
                startRowTile += 1;
                var donViThucHien = sheet.Cells[startRowTile, 1, startRowTile, 5];
                donViThucHien.Merge = true;
                donViThucHien.Value = "Theo đơn vị";
                donViThucHien.Style.Font.Bold = true;
                donViThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                donViThucHien.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 6;
                sheet.Column(2).Width = 50;
                sheet.Column(3).Width = 12;
                sheet.Column(4).Width = 12;
                sheet.Column(5).Width = 12;
                #endregion

                startRow = startRowTile + 2;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol];
                cellRange0.Merge = true;
                cellRange0.Value = "STT";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;

                //Tên đơn vị
                var cellRange1 = sheet.Cells[startRow, startCol + 1, startRow + 1, startCol + 1];
                cellRange1.Merge = true;
                cellRange1.Value = "Tên đơn vị";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu kế hoạch đầu năm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Tổng số chỉ tiêu được điều chỉnh";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                cellRange1 = sheet.Cells[startRow + 1, startCol + 3];
                cellRange1.Value = "Điều chỉnh giảm";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow + 1, startCol + 4];
                cellRange1.Value = "Điều chỉnh tăng";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;


                sheet.Row(startRow).Height = 32;
                sheet.Row(startRow + 1).Height = 32;


                //ExcelPackage p = new ExcelPackage(fileInfo, true);
                ExcelWorksheet ws = sheet;

                #region Set Margion
                ws.PrinterSettings.TopMargin = 0.5m;
                ws.PrinterSettings.BottomMargin = 0.5m;
                ws.PrinterSettings.LeftMargin = 0.7m;
                ws.PrinterSettings.RightMargin = 0.3m;
                ws.PrinterSettings.FitToWidth = 1;
                #endregion

                KeHoachBO khBO = new KeHoachBO();
                var lstdv = khBO.GetListDonViDieuChinh(namKHId);

                int stt = 1;
                startRow = startRow + 2;
                string tendv = "";
                double tongCT = 0;
                int giam = 0;
                int tang = 0;


                foreach (var item in lstdv)
                {
                    var lstkh = kehoachbo.GetbyDV(namKHId, 31, item.DON_VI_ID);
                    List<SumKH> lstdc = kehoachbo.GetDieuChinhTheoDonVi(namKHId, item.DON_VI_ID); //kehoachbo.GetbyDV(namKHId, 61, item.DON_VI_ID);
                    giam = 0;
                    tang = 0;
                    tendv = item.DON_VI;
                    for (int i = 0; i < lstkh.Count; i++)
                    {
                        if (lstkh[i].DON_VI_ID == item.DON_VI_ID)
                        {
                            tongCT = tongCT + 1;
                        }
                    }
                    foreach (SumKH idc in lstdc)
                    {
                        if (idc.GIA_TRI > 0) tang = tang + 1;
                        else if (idc.GIA_TRI < 0) giam = giam + 1;
                    }


                    ws.Cells[startRow, 1].Value = stt;
                    ws.Cells[startRow, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 2].Value = item.DON_VI;
                    ws.Cells[startRow, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 2].Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    ws.Cells[startRow, 3].Value = tongCT;
                    ws.Cells[startRow, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 4].Value = giam;
                    ws.Cells[startRow, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    ws.Cells[startRow, 4].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    ws.Cells[startRow, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

                    ws.Cells[startRow, 5].Value = tang;
                    ws.Cells[startRow, 5].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //ws.Cells[startRow, 5].Style.Numberformat.Format = "###,###,###";
                    ws.Cells[startRow, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    ws.Cells[startRow, 5].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    tongCT = 0;
                    giam = 0;
                    tang = 0;

                    stt = stt + 1;
                    startRow = startRow + 1;
                }

                return excelPackage;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion
        #endregion

        public FileResult ExportTongHopDC(int namKhId)
        {
            ExcelPackage ep = getExcelPackageTongHopDC(namKhId);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "BaoCaoDC-03.xlsx");
            return result;
        }

        private ExcelPackage getExcelPackageTongHopDC(int namKhId)
        {
            #region Data
            int laDonVi = 3;
            int laBan = 2;
            ExportExcellBO exportExcelBO = new ExportExcellBO();
            // danh sach cac don vi de xuat dieu chinh
            //List<DonVi> danhSachDonVi = donvibo.GetListDonViInCommentTable(namKhId, laDonVi);
            List<DonVi> danhSachDonVi = donvibo.GetListDonViDeXuatDieuChinh(namKhId);
            List<DonVi> danhSachDonViKhongDieuChinh = donvibo.GetListDonViKhongDeXuatDieuChinh(namKhId);
            //chi tieu dinh huong dau nam cua dhqg
            List<ExportExcell> chiTieuDinhHuong = exportExcelBO.GetListChiTieuDinhHuongTrongBangComment(namKhId);
            // cac chi tieu dc de xuat dieu chinh
            List<ExportExcell> danhSachChiTieu = exportExcelBO.GetListChiTieuTrongBangComment(namKhId);
            // chi tieu cac don vi de xuat dieu chinh
            Dictionary<string, List<ExportExcell>> chiTieuCacDonVi = new Dictionary<string, List<ExportExcell>>();
            // chi tieu ban duyet de xuat dieu chinh, gia tri co the thay doi hoac khong
            Dictionary<string, List<ExportExcell>> chiTieuCacBan = new Dictionary<string, List<ExportExcell>>();
            Dictionary<string, List<ExportExcell>> chiTieuCacDonViKhongDeXuat = new Dictionary<string, List<ExportExcell>>();
            int len = danhSachDonVi.Count;
            List<ExportExcell> dataDonVi;
            List<ExportExcell> dataBan;
            for (int i = 0; i < len; i++)
            {
                // chi tieu don vi de xuat
                dataDonVi = exportExcelBO.GetListChiTieuDonViTrongBangComment(namKhId, danhSachDonVi[i].DonViId, laDonVi);
                // chi tieu ban da duyet
                dataBan = exportExcelBO.GetListChiTieuDonViTrongBangComment(namKhId, danhSachDonVi[i].DonViId, laBan);
                chiTieuCacDonVi.Add(danhSachDonVi[i].TenVietTat, dataDonVi);
                chiTieuCacBan.Add(danhSachDonVi[i].TenVietTat, dataBan);
            }

            len = danhSachDonViKhongDieuChinh.Count;
            for (int i = 0; i < len; i++)
            {
                dataDonVi = exportExcelBO.GetListChiTieuCacDonViKhongDeXuat(namKhId, danhSachDonViKhongDieuChinh[i].DonViId, 31);
                chiTieuCacDonViKhongDeXuat.Add(danhSachDonViKhongDieuChinh[i].TenVietTat, dataDonVi);
            }
            //List<ExportExcell> doLechDeXuatDonVi = GetDoChenhLechChiTieu(namKhId, 31, 3, danhSachDonVi, danhSachChiTieu);
            //List<ExportExcell> doLechBanChapNhan = GetDoChenhLechChiTieu(namKhId, 31, 2, danhSachDonVi, danhSachChiTieu);
            List<ExportExcell> keHoachDC_DeXuatDonVi = new List<ExportExcell>();
            List<ExportExcell> keHoachDC_BanChapNhan = new List<ExportExcell>();
            ExportExcell item = null;

            for (int i = 0; i < chiTieuDinhHuong.Count; i++)
            {
                item = new ExportExcell();
                item.CHI_TIEU_ID = chiTieuDinhHuong[i].CHI_TIEU_ID;
                item.CAP = chiTieuDinhHuong[i].CAP;
                item.CHO_PHEP_TINH_TONG = chiTieuDinhHuong[i].CHO_PHEP_TINH_TONG;
                item.LA_CHI_TIEU_AO = chiTieuDinhHuong[i].LA_CHI_TIEU_AO;
                item.TEN_CHI_TIEU = chiTieuDinhHuong[i].TEN_CHI_TIEU;
                item.GIA_TRI = 0;
                
                keHoachDC_DeXuatDonVi.Add(item);
            }

            List<ExportExcell> tongChiTieuCacDonViDeXuat = new List<ExportExcell>();
            List<ExportExcell> itemDataDonVi = null;
            for (int i = 0; i < danhSachDonVi.Count; i++)
            {
                itemDataDonVi = chiTieuCacDonVi[danhSachDonVi[i].TenVietTat];
                for (int j = 0; j < itemDataDonVi.Count; j++)
                {
                    if (j < keHoachDC_DeXuatDonVi.Count)
                    {
                        if (keHoachDC_DeXuatDonVi[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            keHoachDC_DeXuatDonVi[j].GIA_TRI += itemDataDonVi[j].GIA_TRI;
                        }
                        
                    }
                    
                }
            }

            for (int i = 0; i < danhSachDonViKhongDieuChinh.Count; i++)
            {
                itemDataDonVi = chiTieuCacDonViKhongDeXuat[danhSachDonViKhongDieuChinh[i].TenVietTat];
                for (int j = 0; j < itemDataDonVi.Count; j++)
                {
                    if (j < keHoachDC_DeXuatDonVi.Count)
                    {
                        if (keHoachDC_DeXuatDonVi[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            keHoachDC_DeXuatDonVi[j].GIA_TRI += itemDataDonVi[j].GIA_TRI;
                        }
                    }
                }
            }

            //for (int i = 0; i < chiTieuDinhHuong.Count; i++)
            //{
            //    item = new ExportExcell();
            //    item.CHI_TIEU_ID = chiTieuDinhHuong[i].CHI_TIEU_ID;
            //    item.CAP = chiTieuDinhHuong[i].CAP;
            //    item.CHO_PHEP_TINH_TONG = chiTieuDinhHuong[i].CHO_PHEP_TINH_TONG;
            //    item.LA_CHI_TIEU_AO = chiTieuDinhHuong[i].LA_CHI_TIEU_AO;
            //    item.TEN_CHI_TIEU = chiTieuDinhHuong[i].TEN_CHI_TIEU;
            //    item.GIA_TRI = 0;
            //    if (chiTieuDinhHuong[i].CAP == 3)
            //    {
            //        item.GIA_TRI = chiTieuDinhHuong[i].GIA_TRI - doLechDeXuatDonVi[i].GIA_TRI;
            //    }
            //    keHoachDC_DeXuatDonVi.Add(item);
            //}

            for (int i = 0; i < chiTieuDinhHuong.Count; i++)
            {
                item = new ExportExcell();
                item.CHI_TIEU_ID = chiTieuDinhHuong[i].CHI_TIEU_ID;
                item.CAP = chiTieuDinhHuong[i].CAP;
                item.CHO_PHEP_TINH_TONG = chiTieuDinhHuong[i].CHO_PHEP_TINH_TONG;
                item.LA_CHI_TIEU_AO = chiTieuDinhHuong[i].LA_CHI_TIEU_AO;
                item.TEN_CHI_TIEU = chiTieuDinhHuong[i].TEN_CHI_TIEU;
                item.GIA_TRI = 0;
                //if (chiTieuDinhHuong[i].CAP == 3)
                //{
                //    item.GIA_TRI = chiTieuDinhHuong[i].GIA_TRI - doLechBanChapNhan[i].GIA_TRI;
                //}
                keHoachDC_BanChapNhan.Add(item);
            }

            for (int i = 0; i < danhSachDonVi.Count; i++)
            {
                itemDataDonVi = chiTieuCacBan[danhSachDonVi[i].TenVietTat];
                for (int j = 0; j < itemDataDonVi.Count; j++)
                {
                    if (j < keHoachDC_BanChapNhan.Count)
                    {
                        if (keHoachDC_BanChapNhan[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            keHoachDC_BanChapNhan[j].GIA_TRI += itemDataDonVi[j].GIA_TRI;
                        }
                    }
                }
            }

            for (int i = 0; i < danhSachDonViKhongDieuChinh.Count; i++)
            {
                itemDataDonVi = chiTieuCacDonViKhongDeXuat[danhSachDonViKhongDieuChinh[i].TenVietTat];
                for (int j = 0; j < itemDataDonVi.Count; j++)
                {
                    if (j < keHoachDC_BanChapNhan.Count)
                    {
                        if (keHoachDC_BanChapNhan[i].CHO_PHEP_TINH_TONG == 1)
                        {
                            keHoachDC_BanChapNhan[j].GIA_TRI += itemDataDonVi[j].GIA_TRI;
                        }
                    }
                }
            }

            #endregion
            ExcelPackage excelPackage = new ExcelPackage();
            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";
            var sheet = excelPackage.Workbook.Worksheets.Add("Tình hình thực hiện kế hoạch " + namKhId);
            sheet.PrinterSettings.TopMargin = topMargin;
            sheet.PrinterSettings.BottomMargin = bottomMargin;
            sheet.PrinterSettings.LeftMargin = leftMargin;
            sheet.PrinterSettings.RightMargin = rightMargin;

            var printArea = sheet.Cells[1, 1, 1000, 20];
            printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

            // output a line for the headers
            sheet.Name = "Tình hình thực hiện KH " + namKhId;

            #region Header
            int startRowLogo = 1;
            var logoCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
            logoCell.Merge = true;
            logoCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
            logoCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
            logoCell.Style.Font.Bold = true;
            logoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            int startRowTitle = startRowLogo + 2;
            int startCol = 1;
            int totalCell = danhSachDonVi.Count * 4 + 6;
            var titleCell = sheet.Cells[startRowTitle, startCol, startRowTitle, totalCell];
            titleCell.Merge = true;
            titleCell.Value = "BC ĐIỀU CHỈNH KẾ HOẠCH NHIỆM VỤ KỲ SƠ KẾT NĂM " + namKhId;
            titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            titleCell.Style.Font.Bold = true;

            int startRow = startRowTitle + 2;
            ExcelRange sttCell = sheet.Cells[startRow, startCol, startRow + 2, startCol];
            sttCell.Merge = true;
            sttCell.Value = "STT";
            sttCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            sttCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            sttCell.Style.Font.Bold = true;
            sttCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ExcelColumn cotChiTieu = sheet.Column(startCol + 1);
            cotChiTieu.Width = 60;
            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
            cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

            ExcelRange chiTieuCell = sheet.Cells[startRow, startCol + 1, startRow + 2, startCol + 1];
            chiTieuCell.Merge = true;
            chiTieuCell.Style.WrapText = true;
            chiTieuCell.Value = "Tên chỉ tiêu (đề xuất điều chỉnh)";
            chiTieuCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            chiTieuCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            chiTieuCell.Style.Font.Bold = true;
            chiTieuCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ExcelRange donViTinhCell = sheet.Cells[startRow, startCol + 2, startRow + 2, startCol + 2];
            donViTinhCell.Merge = true;
            donViTinhCell.Style.WrapText = true;
            donViTinhCell.Value = "Đơn vị tính";
            donViTinhCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            donViTinhCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            donViTinhCell.Style.Font.Bold = true;
            donViTinhCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ExcelRange dhqgCell = sheet.Cells[startRow, startCol + 3, startRow, startCol + 5];
            dhqgCell.Merge = true;
            dhqgCell.Value = "ĐHQGHN";
            dhqgCell.Style.WrapText = true;
            dhqgCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            dhqgCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            dhqgCell.Style.Font.Bold = true;
            dhqgCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ExcelRange dhqgKHDNCell = sheet.Cells[startRow + 1, startCol + 3, startRow + 2, startCol + 3];
            dhqgKHDNCell.Merge = true;
            dhqgKHDNCell.Value = "KH đầu năm";
            dhqgKHDNCell.Style.WrapText = true;
            dhqgKHDNCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            dhqgKHDNCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            dhqgKHDNCell.Style.Font.Bold = true;
            dhqgKHDNCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ExcelRange dhqgKHDCCell = sheet.Cells[startRow + 1, startCol + 4, startRow + 2, startCol + 4];
            dhqgKHDCCell.Merge = true;
            dhqgKHDCCell.Value = "KH ĐC (theo đề xuất của đơn vị)";
            dhqgKHDCCell.Style.WrapText = true;
            dhqgKHDCCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            dhqgKHDCCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            dhqgKHDCCell.Style.Font.Bold = true;
            dhqgKHDCCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ExcelRange dhqgKHDC_Ban_Cell = sheet.Cells[startRow + 1, startCol + 5, startRow + 2, startCol + 5];
            dhqgKHDC_Ban_Cell.Merge = true;
            dhqgKHDC_Ban_Cell.Value = "KH ĐC (theo duyệt của các Ban)";
            dhqgKHDC_Ban_Cell.Style.WrapText = true;
            dhqgKHDC_Ban_Cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            dhqgKHDC_Ban_Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            dhqgKHDC_Ban_Cell.Style.Font.Bold = true;
            dhqgKHDC_Ban_Cell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

            int columnDonVi = startCol + 6;
            int count = danhSachDonVi.Count;
            ExcelRange donVi1_Cell;
            ExcelRange yKienDonViCell;
            ExcelRange yKienBanCell;
            ExcelRange keHoachDCDeXuat;
            ExcelRange coSoDeXuatDC;
            ExcelRange keHoachDCDuyet;
            ExcelRange coSoDuyet;
            for (int i = 0; i < count; i++)
            {
                #region Fill content
                donVi1_Cell = sheet.Cells[startRow, columnDonVi, startRow, columnDonVi + 3];
                donVi1_Cell.Merge = true;
                donVi1_Cell.Value = danhSachDonVi[i].TenVietTat;
                donVi1_Cell.Style.WrapText = true;
                donVi1_Cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                donVi1_Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                donVi1_Cell.Style.Font.Bold = true;
                donVi1_Cell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                yKienDonViCell = sheet.Cells[startRow + 1, columnDonVi, startRow + 1, columnDonVi + 1];
                yKienDonViCell.Merge = true;
                yKienDonViCell.Value = "Ý kiến đơn vị";
                yKienDonViCell.Style.WrapText = true;
                yKienDonViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                yKienDonViCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                yKienDonViCell.Style.Font.Bold = true;
                yKienDonViCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                yKienBanCell = sheet.Cells[startRow + 1, columnDonVi + 2, startRow + 1, columnDonVi + 3];
                yKienBanCell.Merge = true;
                yKienBanCell.Value = "Ý kiến Ban chức năng";
                yKienBanCell.Style.WrapText = true;
                yKienBanCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                yKienBanCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                yKienBanCell.Style.Font.Bold = true;
                yKienBanCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                keHoachDCDeXuat = sheet.Cells[startRow + 2, columnDonVi];
                keHoachDCDeXuat.Merge = true;
                keHoachDCDeXuat.Value = "KH ĐC đề xuất";
                keHoachDCDeXuat.Style.WrapText = true;
                keHoachDCDeXuat.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                keHoachDCDeXuat.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                keHoachDCDeXuat.Style.Font.Bold = true;
                keHoachDCDeXuat.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                coSoDeXuatDC = sheet.Cells[startRow + 2, columnDonVi + 1];
                coSoDeXuatDC.Merge = true;
                coSoDeXuatDC.Value = "Cơ sở đề xuất";
                coSoDeXuatDC.Style.WrapText = true;
                coSoDeXuatDC.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                coSoDeXuatDC.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                coSoDeXuatDC.Style.Font.Bold = true;
                coSoDeXuatDC.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                keHoachDCDuyet = sheet.Cells[startRow + 2, columnDonVi + 2];
                keHoachDCDuyet.Merge = true;
                keHoachDCDuyet.Value = "KH ĐC duyệt";
                keHoachDCDuyet.Style.WrapText = true;
                keHoachDCDuyet.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                keHoachDCDuyet.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                keHoachDCDuyet.Style.Font.Bold = true;
                keHoachDCDuyet.Style.Border.BorderAround(ExcelBorderStyle.Thin);


                coSoDuyet = sheet.Cells[startRow + 2, columnDonVi + 3];
                coSoDuyet.Merge = true;
                coSoDuyet.Value = "Cơ sở duyệt";
                coSoDuyet.Style.WrapText = true;
                coSoDuyet.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                coSoDuyet.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                coSoDuyet.Style.Font.Bold = true;
                coSoDuyet.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                columnDonVi += 4;
                #endregion
            }
            sheet.Row(startRow).Height = 20;
            sheet.Row(startRow + 1).Height = 35;
            sheet.Row(startRow + 2).Height = 75;
            #endregion

            #region Chỉ tiêu
            int headerHeight = 3;
            len = danhSachChiTieu.Count;
            int beginRow = startRow + headerHeight;
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelRange positionMark = null;

            for (int i = 0; i < totalCell; i++)
            {
                positionMark = sheet.Cells[beginRow, i + 1];
                positionMark.Value = "(" + (i + 1) + ")";
                positionMark.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                positionMark.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                positionMark.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            }
            headerHeight += 1;
            beginRow = startRow + headerHeight;
            int stt = 0;
            for (int i = 0; i < len; i++)
            {
                if (danhSachChiTieu[i].LA_CHI_TIEU_AO == 1)
                {
                    continue;
                }

                chiTieuCell = sheet.Cells[beginRow, startCol + 1];
                chiTieuCell.Style.WrapText = true;
                chiTieuCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                chiTieuCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                if (danhSachChiTieu[i].CAP == 1)
                {
                    chiTieuCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    chiTieuCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    chiTieuCell.Value = danhSachChiTieu[i].TEN_CHI_TIEU.ToUpper();
                    chiTieuCell.Style.Font.Bold = true;
                }
                else if (danhSachChiTieu[i].CAP == 2)
                {
                    chiTieuCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    chiTieuCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    chiTieuCell.Value = danhSachChiTieu[i].TEN_CHI_TIEU;
                    chiTieuCell.Style.Font.Bold = true;
                }
                else chiTieuCell.Value = danhSachChiTieu[i].TEN_CHI_TIEU;

                donViTinhCell = sheet.Cells[beginRow, startCol + 2];
                donViTinhCell.Style.WrapText = true;
                donViTinhCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                donViTinhCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                donViTinhCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                if (danhSachChiTieu[i].CAP == 3)
                {
                    donViTinhCell.Value = danhSachChiTieu[i].DON_VI_TINH;
                }
                else
                {
                    donViTinhCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    donViTinhCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                }
                stt++;
                sttCell = sheet.Cells[beginRow, startCol];
                sttCell.Value = stt;
                sttCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                sttCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                sttCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                beginRow++;
            }
            #endregion

            #region DHQGHN
            len = chiTieuDinhHuong.Count;
            beginRow = startRow + headerHeight;
            for (int i = 0; i < len; i++)
            {
                if (chiTieuDinhHuong[i].LA_CHI_TIEU_AO == 1)
                {
                    continue;
                }
                dhqgKHDNCell = sheet.Cells[beginRow, startCol + 3];
                dhqgKHDNCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                dhqgKHDNCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                dhqgKHDNCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                dhqgKHDNCell.Style.WrapText = true;
                if (chiTieuDinhHuong[i].CAP == 3)
                {
                    dhqgKHDNCell.Value = chiTieuDinhHuong[i].GIA_TRI;
                }
                else
                {
                    dhqgKHDNCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    dhqgKHDNCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                }
                beginRow++;
            }
            beginRow = startRow + headerHeight;
            len = keHoachDC_DeXuatDonVi.Count;
            for (int i = 0; i < len; i++)
            {
                if (keHoachDC_DeXuatDonVi[i].LA_CHI_TIEU_AO == 1)
                {
                    continue;
                }
                dhqgKHDCCell = sheet.Cells[beginRow, startCol + 4];
                dhqgKHDCCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                dhqgKHDCCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                dhqgKHDCCell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                dhqgKHDCCell.Style.WrapText = true;
                if (keHoachDC_DeXuatDonVi[i].CAP == 3)
                {
                    dhqgKHDCCell.Value = keHoachDC_DeXuatDonVi[i].GIA_TRI;
                }
                else
                {
                    dhqgKHDCCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    dhqgKHDCCell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                }
                beginRow++;
            }

            beginRow = startRow + headerHeight;
            len = keHoachDC_BanChapNhan.Count;
            for (int i = 0; i < len; i++)
            {
                if (keHoachDC_BanChapNhan[i].LA_CHI_TIEU_AO == 1)
                {
                    continue;
                }
                dhqgKHDC_Ban_Cell = sheet.Cells[beginRow, startCol + 5];
                dhqgKHDC_Ban_Cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                dhqgKHDC_Ban_Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                dhqgKHDC_Ban_Cell.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                dhqgKHDC_Ban_Cell.Style.WrapText = true;
                if (keHoachDC_BanChapNhan[i].CAP == 3)
                {
                    dhqgKHDC_Ban_Cell.Value = keHoachDC_BanChapNhan[i].GIA_TRI;
                }
                else
                {
                    dhqgKHDC_Ban_Cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    dhqgKHDC_Ban_Cell.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                }
                beginRow++;
            }
            #endregion

            len = danhSachDonVi.Count;
            columnDonVi = startCol + 6;
            for (int i = 0; i < len; i++)
            {
                dataDonVi = chiTieuCacDonVi[danhSachDonVi[i].TenVietTat];
                beginRow = startRow + headerHeight;
                for (int j = 0; j < dataDonVi.Count; j++)
                {
                    if (dataDonVi[j].LA_CHI_TIEU_AO == 1)
                    {
                        continue;
                    }
                    keHoachDCDeXuat = sheet.Cells[beginRow, columnDonVi];
                    keHoachDCDeXuat.Style.WrapText = true;
                    keHoachDCDeXuat.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    keHoachDCDeXuat.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    coSoDeXuatDC = sheet.Cells[beginRow, columnDonVi + 1];
                    coSoDeXuatDC.Style.WrapText = true;
                    coSoDeXuatDC.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    coSoDeXuatDC.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    if (dataDonVi[j].CAP == 3)
                    {
                        keHoachDCDeXuat.Value = dataDonVi[j].GIA_TRI;
                        coSoDeXuatDC.Value = dataDonVi[j].COMMENT;
                    }
                    else
                    {
                        keHoachDCDeXuat.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        keHoachDCDeXuat.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        coSoDeXuatDC.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        coSoDeXuatDC.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    beginRow++;
                }

                dataBan = chiTieuCacBan[danhSachDonVi[i].TenVietTat];
                beginRow = startRow + headerHeight;
                int dem = -1;
                foreach (ExportExcell e in dataBan)
                {
                    dem++;
                    if (dataBan[dem].LA_CHI_TIEU_AO == 1)
                    {
                        continue;
                    }
                    keHoachDCDuyet = sheet.Cells[beginRow, columnDonVi + 2];
                    keHoachDCDuyet.Style.WrapText = true;
                    keHoachDCDuyet.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    keHoachDCDuyet.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    coSoDuyet = sheet.Cells[beginRow, columnDonVi + 3];
                    coSoDuyet.Style.WrapText = true;
                    coSoDuyet.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                    coSoDuyet.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    if (e.CAP == 3)
                    {
                        keHoachDCDuyet.Value = e.GIA_TRI;
                        coSoDuyet.Value = e.COMMENT;
                    }
                    else
                    {
                        keHoachDCDuyet.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        keHoachDCDuyet.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        coSoDuyet.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        coSoDuyet.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                    }
                    beginRow++;
                }
                columnDonVi += 4;
            }
            return excelPackage;
        }

        private List<ExportExcell> GetDieuChinhDHQG_TongHop(int namKhId, int statusId_KH, int statusId_CM, List<DonVi> danhSachDonVi, List<ExportExcell> danhSachChiTieu)
        {
            List<ExportExcell> keHoachDieuChinhTheoDV = new List<ExportExcell>();
            ExportExcellBO sql = new ExportExcellBO();
            ExportExcell keHoach;
            ExportExcell dieuChinh;
            double valueKH = 0;
            double valueDC = 0;
            ExportExcell result = null;
            ExportExcell temp = null;
            foreach (ExportExcell ex in danhSachChiTieu)
            {
                if (ex.CAP == 3)
                {
                    valueKH = 0;
                    valueDC = 0;
                    foreach (DonVi d in danhSachDonVi)
                    {
                        keHoach = sql.GetByChiTieuIdInKeHoachTable(namKhId, d.DonViId, statusId_KH, ex.CHI_TIEU_ID);
                        dieuChinh = sql.GetByChiTieuIdInCommentTable(namKhId, d.DonViId, statusId_CM, ex.CHI_TIEU_ID);
                        valueKH += keHoach.GIA_TRI;
                        valueDC += dieuChinh.GIA_TRI;
                    }
                    result = new ExportExcell();
                    result.CAP = ex.CAP;
                    result.CHI_TIEU_ID = ex.CHI_TIEU_ID;
                    result.GIA_TRI = valueKH - valueDC;

                    keHoachDieuChinhTheoDV.Add(result);
                }
                else
                {
                    temp = new ExportExcell();
                    temp.CAP = ex.CAP;
                    temp.CHI_TIEU_ID = ex.CHI_TIEU_ID;
                    temp.MA = ex.MA;

                    keHoachDieuChinhTheoDV.Add(temp);
                }
            }

            return keHoachDieuChinhTheoDV;
        }

        private List<ExportExcell> GetDoChenhLechChiTieu(int namKhId, int statusId_KH, int statusId_CM, List<DonVi> danhSachDonVi, List<ExportExcell> danhSachChiTieu)
        {
            List<ExportExcell> keHoachDieuChinhTheoDV = new List<ExportExcell>();
            ExportExcellBO sql = new ExportExcellBO();
            ExportExcell keHoach;
            ExportExcell dieuChinh;
            double valueKH = 0;
            double valueDC = 0;
            ExportExcell result = null;
            foreach (ExportExcell ex in danhSachChiTieu)
            {
                result = new ExportExcell();
                if (ex.CAP == 3)
                {
                    valueKH = 0;
                    valueDC = 0;
                    foreach (DonVi d in danhSachDonVi)
                    {
                        sql = new ExportExcellBO();
                        keHoach = sql.GetByChiTieuIdInKeHoachTable(namKhId, d.DonViId, statusId_KH, ex.CHI_TIEU_ID);
                        sql = new ExportExcellBO();
                        dieuChinh = sql.GetByChiTieuIdInCommentTable(namKhId, d.DonViId, statusId_CM, ex.CHI_TIEU_ID);
                        if (keHoach != null)
                        {
                            valueKH += keHoach.GIA_TRI;
                        }

                        if (dieuChinh == null)
                        {
                            sql = new ExportExcellBO();
                            dieuChinh = sql.GetByChiTieuIdInKeHoachTable(namKhId, d.DonViId, statusId_KH, ex.CHI_TIEU_ID);
                        }

                        if (dieuChinh != null)
                        {
                            valueDC += dieuChinh.GIA_TRI;
                        }
                    }
                    result.CAP = ex.CAP;
                    result.CHI_TIEU_ID = ex.CHI_TIEU_ID;
                    result.GIA_TRI = valueKH - valueDC;
                }
                else
                {
                    result.GIA_TRI = 0;
                    result.CAP = ex.CAP;
                    result.CHI_TIEU_ID = ex.CHI_TIEU_ID;
                    result.MA = ex.MA;
                }
                keHoachDieuChinhTheoDV.Add(result);
            }

            return keHoachDieuChinhTheoDV;
        }

        private DonViModel GetDonVi()
        {
            NguoiDungModel nguoiDungInfo = Session["NguoiDungInfo"] as NguoiDungModel;
            DonViModel donViModel = new DonViModel();
            if (nguoiDungInfo.listDonVi != null && nguoiDungInfo.listDonVi.Count > 0)
            {
                donViModel = nguoiDungInfo.listDonVi[0];
            }

            return donViModel;
        }

        #region Khung chi tieu dau nam
        public FileResult ExporBanHanhKhungCTDonViAll(int namKHId, string namKH, int nhomDonViId)
        {
            ExcelPackage ep = this.getExcelPackBanHanhKhungCTDauNamAll(namKHId, namKH, nhomDonViId);
            FileResult result = File(ep.GetAsByteArray(), "application/octet-stream", "Ke hoach dau nam.xlsx");
            return result;
        }

        public FileResult PrintBanHanhKhungCTDonViAll(int namKHId, string namKH, int nhomDonViId)
        {
            ExcelPackage p = getExcelPackBanHanhKhungCTDauNamAll(namKHId, namKH, nhomDonViId);

            MemoryStream inStream = new MemoryStream(p.GetAsByteArray());

            XlsFile Result11 = new XlsFile(1, true);

            Result11.Open(inStream);
            Result11.PrintPaperSize = TPaperSize.A4;
            if (!isLandScape) Result11.PrintLandscape = false;
            else Result11.PrintLandscape = true;
            Result11.AutofitRowsOnWorkbook(true, true, 1);
            using (FlexCelPdfExport pdf = new FlexCelPdfExport())
            {
                pdf.Workbook = Result11;
                pdf.FontEmbed = FlexCel.Pdf.TFontEmbed.Embed;
                using (MemoryStream ms = new MemoryStream())
                {
                    pdf.BeginExport(ms);
                    pdf.ExportAllVisibleSheets(true, "DieuChinhChiTieuKeHoachDonVi");
                    pdf.EndExport();
                    ms.Position = 0;
                    return File(ms.ToArray(), "application/pdf");
                }
            }
            p.Dispose();
            inStream.Dispose();

            return null;

        }

        public ExcelPackage getExcelPackBanHanhKhungCTDauNamAll(int namKHId, string namKH, int nhomDonViId)
        {
            lstTGT = new List<TongGiaTri>();
            string tenNhomDV = string.Empty;
            ExportExcellBO sqlBO = new ExportExcellBO();
            int laChiTieuChinhThuc = 31;
            int laChiTieuDieuChinh = 61;
            List<ExportExcell> chiTieuChinhThuc;

            int donViId;
            string tenDV = "";
            Color backGroundColor = Color.FromArgb(208, 206, 206);
            ExcelPackage excelPackage = new ExcelPackage();

            excelPackage.Workbook.Properties.Author = "vanduong";
            excelPackage.Workbook.Properties.Title = "Export excel";

            DonViBO donviBO = new DonViBO();
            List<DonVi> listDonVi = donviBO.GetAllDonViByNhom(nhomDonViId);

            foreach (DonVi iDonVi in listDonVi)
            {
                donViId = iDonVi.DonViId;
                donviBO = new DonViBO();
                DonVi dvbc = donviBO.GetById(donViId);
                if (dvbc != null) tenDV = dvbc.TenVietTat;

                ExportExcellBO exBO = new ExportExcellBO();
                List<ExportExcell> chiTieuChinhThucDHQG = sqlBO.GetListChiTieuDHQG(namKHId, laChiTieuChinhThuc);

                chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, 0);

                //bool coKeHoachDC = exBO.DaDieuChinh(namKHId, laChiTieuDieuChinh);
                //if (coKeHoachDC)
                //{
                //    chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuDieuChinh, donViId, 0);
                //}
                //else
                //{
                //    chiTieuChinhThuc = sqlBO.GetListChiTieuDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, 0);
                //}

                //List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom(namKHId, laChiTieuChinhThuc, donViId, 0);
                //List<ExportExcell> keHoachThucHien = sqlBO.GetListKeHoachThucHienDonVi_TheoNhom2(namKHId, laChiTieuChinhThuc, donViId, mocKS, 0);
                List<ExportExcell> donViPhuTrach = sqlBO.GetListBanPhuTrach_TheoDonVi(namKHId, 0, donViId);

                List<ExportExcell> chiTieuRiengDauNam = sqlBO.GetListChiTieuRiengDonVi_KeHoach(namKHId, donViId, 21);
                List<ExportExcell> chiTieuRiengThucHien = sqlBO.GetListChiTieuRiengDonVi_KeHoachThucHien(namKHId, donViId);

                var sheet = excelPackage.Workbook.Worksheets.Add(tenDV);
                sheet.PrinterSettings.TopMargin = topMargin;
                sheet.PrinterSettings.BottomMargin = bottomMargin;
                sheet.PrinterSettings.LeftMargin = leftMargin;
                sheet.PrinterSettings.RightMargin = rightMargin;

                var printArea = sheet.Cells[1, 1, chiTieuChinhThuc.Count + chiTieuRiengDauNam.Count + 5, 10];
                printArea.Style.Font.SetFromFont(new Font("Times New Roman", 12));

                // output a line for the headers
                sheet.Name = tenDV;
                int startRowLogo = 1;
                var donViCell = sheet.Cells[startRowLogo, 1, startRowLogo, 3];
                donViCell.Merge = true;
                donViCell.Value = "ĐẠI HỌC QUỐC GIA HÀ NỘI";
                donViCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                donViCell.Style.Font.Bold = true;
                donViCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                int startRowTile = 3;

                var titleCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                //if (hasDieuChinh) titleCell = sheet.Cells[startRowTile, 1, startRowTile, 10];

                titleCell.Merge = true;
                titleCell.Value = "KHUNG CHỈ TIÊU KẾ HOẠCH NHIỆM VỤ NĂM " + namKH;
                titleCell.Style.Font.SetFromFont(new Font("Times New Roman", 14));
                titleCell.Style.Font.Bold = true;
                titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                #region Thong tin moc kiem soat
                //int namkh = 0;
                //if (!string.IsNullOrEmpty(namKH))
                //{
                //    try
                //    {
                //        namkh = System.Convert.ToInt32(namKH);
                //    }
                //    catch { }
                //}
                //MocKiemSoatBO mc = new MocKiemSoatBO();
                //MocKiemSoat mks = mc.GetbyID(mocKS); //mc.GetMocKSIsActive(namkh);

                //string kyBC = string.Empty;
                //if (mks != null)
                //{
                //    kyBC = mks.MOC_KIEM_SOAT;
                //}
                //startRowTile += 1;
                //var kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 8];
                ////if (hasDieuChinh) kyBaoCaoCell = sheet.Cells[startRowTile, 1, startRowTile, 9];
                //kyBaoCaoCell.Merge = true;
                //kyBaoCaoCell.Value = "Kỳ báo cáo: " + kyBC;
                //kyBaoCaoCell.Style.Font.Bold = true;
                //kyBaoCaoCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                //kyBaoCaoCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                #endregion

                #region Set Column Width
                sheet.Column(1).Width = 4;
                sheet.Column(2).Width = 5;
                sheet.Column(3).Width = 44;
                //if (hasDieuChinh) sheet.Column(3).Width = 28;

                sheet.Column(4).Width = 10;
                sheet.Column(5).Width = 10;
                sheet.Column(6).Width = 11;
                sheet.Column(7).Width = 11;
                //sheet.Column(8).Width = 8;
                //sheet.Column(9).Width = 8;
                //sheet.Column(10).Width = 8;
                #endregion

                startRow = startRowTile + 1;
                //STT
                var cellRange0 = sheet.Cells[startRow, startCol, startRow + 1, startCol + 1];
                cellRange0.Merge = true;
                cellRange0.Value = "A";
                cellRange0.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange0.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange0.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange0.Style.Font.Bold = true;
                sheet.Column(startCol).Style.WrapText = true;
                sheet.Column(startCol + 1).Style.WrapText = true;


                //Chitieu
                var cellRange1 = sheet.Cells[startRow, startCol + 2, startRow + 1, startCol + 2];
                cellRange1.Merge = true;
                cellRange1.Value = "CÁC CHỈ TIÊU CHUNG";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                sheet.Column(startCol + 2).Style.WrapText = true;

                //Don vi giam sat
                cellRange1 = sheet.Cells[startRow, startCol + 3, startRow + 1, startCol + 3];
                cellRange1.Merge = true;
                cellRange1.Value = "ĐV phân bổ và giám sát";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                cellRange1 = sheet.Cells[startRow, startCol + 4, startRow + 1, startCol + 4];
                cellRange1.Merge = true;
                cellRange1.Value = "Đơn vị tính";
                cellRange1.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange1.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                cellRange1.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange1.Style.Font.Bold = true;
                cellRange1.Style.WrapText = true;

                var cellRange2 = sheet.Cells[startRow, startCol + 5, startRow + 1, startCol + 5];
                cellRange2.Merge = true;
                cellRange2.Value = "ĐHQGHN";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                cellRange2 = sheet.Cells[startRow, startCol + 6, startRow + 1, startCol + 6];
                cellRange2.Merge = true;
                cellRange2.Value = tenDV;// "ĐHQGHN";
                cellRange2.Style.Font.Bold = true;
                cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                sheet.Row(startRow).Height = 30;
                sheet.Row(startRow + 1).Height = 30;

                int beginRow = startRow + 1;
                int beginCol = startCol;
                int sttLevel1 = 1;
                int sttLevel2 = 1;
                bool chiTieuAo = false;
                bool cungDonViTinh = false;
                string currDonViTinh = "";
                double tongKH = 0;
                double tongTH = 0;
                string dvPhuTrachCTCap2 = "";
                int rowIndexCTCap2 = 0;
                for (int i = 0; i < chiTieuChinhThuc.Count; i++)
                {
                    //if (chiTieuChinhThucDHQG[i].CAP == 1 ||
                    //    chiTieuChinhThucDHQG[i].CAP == 2)
                    if (chiTieuChinhThuc[i].CAP == 1)
                    {
                        #region Chi tieu cap 1
                        beginRow++;
                        cellRange2 = sheet.Cells[beginRow, startCol, beginRow, startCol + 4];
                        cellRange2.Merge = true;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU.ToUpper();

                        //ĐHQG
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        //Đơn vị
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        ////%(KH)
                        //cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                        #endregion
                    }
                    else if (chiTieuChinhThuc[i].CAP == 2)
                    {
                        if (chiTieuChinhThuc[i].LA_CHI_TIEU_AO == 1)
                        {
                            chiTieuAo = true;
                        }
                        else
                        {
                            #region In Tong chi tieu (neu co)
                            if ((cungDonViTinh) && (currDonViTinh != "%"))
                            {
                                //cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                                //cellRange2.Value = dvPhuTrachCTCap2;

                                //cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                                //cellRange2.Value = currDonViTinh;

                                //cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                                //cellRange2.Value = tongKH;

                                //if ((tongKH < 1) && (tongKH > 0))
                                //{
                                //    cellRange2.Style.Numberformat.Format = "0.##";
                                //}
                                //else if (tongKH % 1 == 0)
                                //{
                                //    cellRange2.Style.Numberformat.Format = "###,###,###";
                                //}
                                //else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                //cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                                //cellRange2.Value = tongTH;
                                //if (tongTH % 1 == 0)
                                //{
                                //    cellRange2.Style.Numberformat.Format = "###,###,###";
                                //}
                                //else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                                //cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                                //cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                                //cungDonViTinh = false;
                            }
                            #endregion

                            beginRow++;

                            rowIndexCTCap2 = beginRow;
                            cungDonViTinh = true;
                            currDonViTinh = "";
                            tongKH = 0;
                            tongTH = 0;

                            chiTieuAo = false;
                            #region Header
                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = "";

                            //Ten chi tieu
                            cellRange2 = sheet.Cells[beginRow, startCol + 2];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;

                            //Don vi giam sat
                            cellRange2 = sheet.Cells[beginRow, startCol + 3];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Style.WrapText = true;

                            //Don vi tinh
                            cellRange2 = sheet.Cells[beginRow, startCol + 4];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            #endregion

                            //ĐHQG
                            cellRange2 = sheet.Cells[beginRow, startCol + 5];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";
                            //Đơn vị
                            cellRange2 = sheet.Cells[beginRow, startCol + 6];
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            //cellRange2.Style.Numberformat.Format = "###,###,###";

                            ////%(KH)
                            //cellRange2 = sheet.Cells[beginRow, startCol + 7];
                            //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            //cellRange2.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            //cellRange2.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                            //cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;
                        }
                    }
                    else //if (chiTieuChinhThucDHQG[i].CAP > 2)
                    {
                        beginRow++;
                        //chiTieuAo = false;
                        if (chiTieuAo)
                        {
                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellRange2.Value = sttLevel1.ToString();

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = "";// (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            sttLevel1 = sttLevel1 + 1;
                            sttLevel2 = 1;

                        }
                        else
                        {
                            if (sttLevel2 == 1)
                            {
                                dvPhuTrachCTCap2 = donViPhuTrach[i].DON_VI;
                                currDonViTinh = chiTieuChinhThuc[i].DON_VI_TINH;
                                cungDonViTinh = true;
                                tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                //tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                            }
                            else
                            {
                                if (cungDonViTinh)
                                {
                                    if ((currDonViTinh != null) && (chiTieuChinhThuc[i].DON_VI_TINH != null) &&
                                        (currDonViTinh.Trim().ToUpper() == chiTieuChinhThuc[i].DON_VI_TINH.Trim().ToUpper()))
                                    {
                                        cungDonViTinh = true;
                                        tongKH = tongKH + chiTieuChinhThuc[i].GIA_TRI;
                                        //tongTH = tongTH + keHoachThucHien[i].GIA_TRI;
                                    }
                                    else
                                    {
                                        cungDonViTinh = false;
                                    }
                                }

                                if (dvPhuTrachCTCap2 != donViPhuTrach[i].DON_VI) dvPhuTrachCTCap2 = "";
                            }

                            //STT
                            cellRange2 = sheet.Cells[beginRow, startCol];
                            cellRange2.Style.Font.Bold = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cellRange2 = sheet.Cells[beginRow, startCol + 1];
                            cellRange2.Merge = true;
                            cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellRange2.Value = (sttLevel1 - 1).ToString() + "." + sttLevel2.ToString();
                            cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            sttLevel2 = sttLevel2 + 1;
                        }

                        //Ten chi tieu
                        cellRange2 = sheet.Cells[beginRow, startCol + 2];
                        cellRange2.Value = chiTieuChinhThuc[i].TEN_CHI_TIEU;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        //Don vi giam sat
                        cellRange2 = sheet.Cells[beginRow, startCol + 3];
                        //cellRange2.Value = "Đơn vị giám sát";
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.WrapText = true;

                        if (i < donViPhuTrach.Count) cellRange2.Value = donViPhuTrach[i].DON_VI ?? string.Empty;

                        //Don vi tinh
                        cellRange2 = sheet.Cells[beginRow, startCol + 4];
                        cellRange2.Value = chiTieuChinhThuc[i].DON_VI_TINH;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cellRange2.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        cellRange2.Style.WrapText = true;


                        //ĐHQG
                        cellRange2 = sheet.Cells[beginRow, startCol + 5];
                        cellRange2.Value = chiTieuChinhThucDHQG[i].GIA_TRI;// chiTieuChinhThuc[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((chiTieuChinhThucDHQG[i].GIA_TRI < 1) && (chiTieuChinhThucDHQG[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuChinhThucDHQG[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";


                        //Đơn vị
                        cellRange2 = sheet.Cells[beginRow, startCol + 6];
                        cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI;
                        cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                        if ((chiTieuChinhThuc[i].GIA_TRI < 1) && (chiTieuChinhThuc[i].GIA_TRI > 0))
                        {
                            cellRange2.Style.Numberformat.Format = "0.##";
                        }
                        else if (chiTieuChinhThuc[i].GIA_TRI % 1 == 0)
                        {
                            cellRange2.Style.Numberformat.Format = "###,###,###";
                        }
                        else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                        ////%(KH)
                        //cellRange2 = sheet.Cells[beginRow, startCol + 7];
                        //cellRange2.Value = chiTieuChinhThuc[i].GIA_TRI > 0 ?
                        //    Math.Round(((float)keHoachThucHien[i].GIA_TRI / chiTieuChinhThuc[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0;
                        //cellRange2.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cellRange2.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        ////cellRange2.Style.Numberformat.Format = "###.##";

                    }
                }
                #region In Tong chi tieu (neu co)
                if ((cungDonViTinh) && (currDonViTinh != "%"))
                {
                    //cellRange2 = sheet.Cells[rowIndexCTCap2, 4];
                    //cellRange2.Value = dvPhuTrachCTCap2;

                    //cellRange2 = sheet.Cells[rowIndexCTCap2, 5];
                    //cellRange2.Value = currDonViTinh;

                    //cellRange2 = sheet.Cells[rowIndexCTCap2, 6];
                    //cellRange2.Value = tongKH;

                    //if ((tongKH < 1) && (tongKH > 0))
                    //{
                    //    cellRange2.Style.Numberformat.Format = "0.##";
                    //}
                    //else if (tongKH % 1 == 0)
                    //{
                    //    cellRange2.Style.Numberformat.Format = "###,###,###";
                    //}
                    //else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    //cellRange2 = sheet.Cells[rowIndexCTCap2, 7];
                    //cellRange2.Value = tongTH;
                    //if (tongTH % 1 == 0)
                    //{
                    //    cellRange2.Style.Numberformat.Format = "###,###,###";
                    //}
                    //else cellRange2.Style.Numberformat.Format = "###,###,###.##";

                    //cellRange2 = sheet.Cells[rowIndexCTCap2, 8];
                    //cellRange2.Value = Math.Round((tongTH / tongKH) * 100, MidpointRounding.AwayFromZero);
                    //cungDonViTinh = false;
                }
                #endregion

                #region Chi tieu rieng

                if (chiTieuRiengDauNam.Count > 0)
                {
                    int startRowChiTieuRieng = beginRow + 1;

                    ExcelRange cotStt = sheet.Cells[startRowChiTieuRieng, 1, startRowChiTieuRieng, 2];
                    ExcelRange cotChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 2];
                    ExcelRange cotDonviTinh = sheet.Cells[startRowChiTieuRieng, startCol + 3, startRowChiTieuRieng, startCol + 4];
                    //ExcelRange cotGiaTriDHQG = sheet.Cells[startRowChiTieuRieng, startCol + 5, startRowChiTieuRieng, startCol + 5];
                    ExcelRange cotGiaTriChiTieu = sheet.Cells[startRowChiTieuRieng, startCol + 5, startRowChiTieuRieng, startCol + 6];
                    //ExcelRange cotThucHien = sheet.Cells[startRowChiTieuRieng, startCol + 5];
                    //ExcelRange cotTiLe = sheet.Cells[startRowChiTieuRieng, startCol + 7];

                    sheet.Row(startRowChiTieuRieng).Height = 40;

                    cotStt.Merge = true;
                    cotStt.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotStt.Value = "B";
                    cotStt.Style.Font.Bold = true;
                    cotStt.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotStt.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotChiTieu.Value = "CÁC CHỈ TIÊU ĐẶC THÙ CỦA ĐƠN VỊ";
                    cotChiTieu.Style.Font.Bold = true;
                    cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotChiTieu.Style.WrapText = true;
                    cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotDonviTinh.Merge = true;
                    cotDonviTinh.Value = "Đơn vị tính";
                    cotDonviTinh.Style.Font.Bold = true;
                    cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotDonviTinh.Style.WrapText = true;
                    cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //cotGiaTriDHQG.Merge = true;
                    //cotGiaTriDHQG.Value = "Mức chỉ tiêu năm " + namKH + " của ĐHQG";
                    //cotGiaTriDHQG.Style.Font.Bold = true;
                    //cotGiaTriDHQG.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cotGiaTriDHQG.Style.WrapText = true;
                    //cotGiaTriDHQG.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    //cotGiaTriDHQG.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    cotGiaTriChiTieu.Merge = true;
                    cotGiaTriChiTieu.Value = "Mức chỉ tiêu năm " + namKH + " đơn vị đăng ký";
                    cotGiaTriChiTieu.Style.Font.Bold = true;
                    cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    cotGiaTriChiTieu.Style.WrapText = true;
                    cotGiaTriChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                    //cotThucHien.Style.Font.Bold = true;
                    //cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cotThucHien.Style.WrapText = true;

                    //cotTiLe.Style.Font.Bold = true;
                    //cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    //cotTiLe.Style.WrapText = true;
                    beginRow = startRowChiTieuRieng;
                    int stt = 0;
                    ExcelRange cellSTT = null;
                    for (int i = 0; i < chiTieuRiengDauNam.Count; i++)
                    {
                        if (chiTieuRiengDauNam[i].LA_CHI_TIEU_AO == 1)
                        {
                            continue;
                        }
                        beginRow++;

                        if (chiTieuRiengDauNam[i].CAP == 1)
                        {
                            cotChiTieu = sheet.Cells[beginRow, startCol, beginRow, 5];
                            cotChiTieu.Merge = true;
                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU.ToUpper();
                            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;

                            //cotGiaTriDHQG = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 6];
                            //cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            //cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            //cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            //cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            //cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //cotGiaTriDHQG.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            //cotGiaTriDHQG.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Merge = true;
                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            //cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else if (chiTieuRiengDauNam[i].CAP == 2)
                        {
                            stt++;
                            cellSTT = sheet.Cells[beginRow, 1];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellSTT.Value = (stt).ToString();

                            cellSTT = sheet.Cells[beginRow, 2];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);

                            cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            //cotGiaTriDHQG = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 6];
                            //cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            //cotTiLe = sheet.Cells[beginRow, startCol + 7];


                            cotChiTieu.Style.WrapText = true;
                            cotChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                            cotChiTieu.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                            cotChiTieu.Style.Font.Bold = true;

                            cotDonviTinh.Merge = true;

                            cotDonviTinh.Style.WrapText = true;
                            cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                            cotGiaTriChiTieu.Merge = true;
                            cotGiaTriChiTieu.Style.WrapText = true;
                            cotGiaTriChiTieu.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            //cotThucHien.Style.WrapText = true;
                            //cotThucHien.Style.VerticalAlignment = ExcelVerticalAlignment.Center;


                            //cotTiLe.Style.WrapText = true;
                            //cotTiLe.Style.VerticalAlignment = ExcelVerticalAlignment.Center;

                            cotChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotDonviTinh.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotDonviTinh.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //cotGiaTriDHQG.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            //cotGiaTriDHQG.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            cotGiaTriChiTieu.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            cotGiaTriChiTieu.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //cotThucHien.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            //cotThucHien.Style.Fill.BackgroundColor.SetColor(backGroundColor);

                            //cotTiLe.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            //cotTiLe.Style.Fill.BackgroundColor.SetColor(backGroundColor);
                        }
                        else
                        {
                            //cotDonviTinh.Value = (chiTieuRiengDauNam[i].DON_VI_TINH.ToUpper() == "NA" ?
                            //    string.Empty : chiTieuRiengDauNam[i].DON_VI_TINH);
                            stt++;

                            cellSTT = sheet.Cells[beginRow, 1];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                            cellSTT.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                            cellSTT.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            cellSTT.Value = (stt).ToString();


                            cellSTT = sheet.Cells[beginRow, 2];
                            cellSTT.Style.Border.BorderAround(ExcelBorderStyle.Thin);


                            cotChiTieu = sheet.Cells[beginRow, startCol + 2];
                            cotDonviTinh = sheet.Cells[beginRow, startCol + 3, beginRow, startCol + 4];
                            //cotGiaTriDHQG = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 5];
                            cotGiaTriChiTieu = sheet.Cells[beginRow, startCol + 5, beginRow, startCol + 6];
                            //cotThucHien = sheet.Cells[beginRow, startCol + 6];
                            //cotTiLe = sheet.Cells[beginRow, startCol + 7];

                            cotChiTieu.Value = chiTieuRiengDauNam[i].TEN_CHI_TIEU;
                            cotDonviTinh.Value = chiTieuRiengDauNam[i].DON_VI_TINH;
                            cotDonviTinh.Merge = true;

                            cotGiaTriChiTieu.Merge = true;
                            cotGiaTriChiTieu.Value = chiTieuRiengDauNam[i].GIA_TRI;
                            if ((chiTieuRiengDauNam[i].GIA_TRI < 1) && (chiTieuRiengDauNam[i].GIA_TRI > 0))
                            {
                                cotGiaTriChiTieu.Style.Numberformat.Format = "0.##";
                            }
                            else if (chiTieuRiengDauNam[i].GIA_TRI % 1 == 0)
                            {
                                cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###";
                            }
                            else cotGiaTriChiTieu.Style.Numberformat.Format = "###,###,###.##";

                            //cotThucHien.Value = chiTieuRiengThucHien[i].GIA_TRI;
                            ////cotThucHien.Style.Numberformat.Format = "###,###,###";
                            //if (chiTieuRiengThucHien[i].GIA_TRI % 1 == 0)
                            //{
                            //    cotThucHien.Style.Numberformat.Format = "###,###,###";
                            //}
                            //else cotThucHien.Style.Numberformat.Format = "###,###,###.##";

                            //cotTiLe.Value = (chiTieuRiengDauNam[i].GIA_TRI > 0 ?
                            //    Math.Round((chiTieuRiengThucHien[i].GIA_TRI / chiTieuRiengDauNam[i].GIA_TRI) * 100, MidpointRounding.AwayFromZero) : 0);
                        }

                        cotChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cotThucHien.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cotTiLe.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        //cotGiaTriDHQG.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotGiaTriChiTieu.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                        cotDonviTinh.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        cotDonviTinh.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    }
                }
                #endregion
            }
            return excelPackage;
        }

        #endregion
    
        private double String2Double(string input)
        {
            try
            {
                if (string.IsNullOrEmpty(input)) return 0;
                else
                {
                    return System.Convert.ToDouble(input);
                }
            }
            catch
            {
                return 0;
            }
        }
    
    }
}